21#if defined(PDF_ENABLE_SKIA) && !defined(PDF_USE_SKIA)
25#include "public/cpp/fpdf_scopers.h"
26#include "public/fpdf_annot.h"
27#include "public/fpdf_attachment.h"
28#include "public/fpdf_dataavail.h"
29#include "public/fpdf_edit.h"
30#include "public/fpdf_ext.h"
31#include "public/fpdf_formfill.h"
32#include "public/fpdf_progressive.h"
33#include "public/fpdf_structtree.h"
34#include "public/fpdf_text.h"
35#include "public/fpdfview.h"
36#include "samples/helpers/dump.h"
37#include "samples/helpers/event.h"
38#include "samples/helpers/page_renderer.h"
39#include "samples/helpers/write.h"
40#include "testing/command_line_helpers.h"
41#include "testing/font_renamer.h"
42#include "testing/fx_string_testhelpers.h"
43#include "testing/test_loader.h"
44#include "testing/utils/file_util.h"
45#include "testing/utils/hash.h"
46#include "testing/utils/path_service.h"
47#include "third_party/abseil-cpp/absl/types/optional.h"
48#include "third_party/base/check_op.h"
52#include <errhandlingapi.h>
56#include "samples/helpers/win32/com_factory.h"
57#include "third_party/base/win/scoped_select_object.h"
62#ifdef ENABLE_CALLGRIND
63#include <valgrind/callgrind.h>
66#if defined(PDF_USE_PARTITION_ALLOC)
67#include "testing/allocator_shim_config.h"
71#include "third_party/skia/include/core/SkCanvas.h"
72#include "third_party/skia/include/core/SkColor.h"
73#include "third_party/skia/include/core/SkDocument.h"
74#include "third_party/skia/include/core/SkPicture.h"
75#include "third_party/skia/include/core/SkPictureRecorder.h"
76#include "third_party/skia/include/core/SkPixmap.h"
77#include "third_party/skia/include/core/SkRefCnt.h"
78#include "third_party/skia/include/core/SkStream.h"
79#include "third_party/skia/include/core/SkSurface.h"
82#include "third_party/skia/include/docs/SkXPSDocument.h"
85#ifdef BUILD_WITH_CHROMIUM
86#include "samples/chromium_support/discardable_memory_allocator.h"
91#include "testing/v8_initializer.h"
92#include "v8/include/libplatform/libplatform.h"
93#include "v8/include/v8-array-buffer.h"
94#include "v8/include/v8-isolate.h"
95#include "v8/include/v8-platform.h"
96#include "v8/include/v8-snapshot.h"
100#define access _access
101#define snprintf _snprintf
107#if defined(__APPLE__) || (defined(__linux__
) && !defined(__ANDROID__))
108#define WORDEXP_AVAILABLE
111#ifdef WORDEXP_AVAILABLE
117enum class RendererType {
123#if defined(PDF_ENABLE_SKIA)
128enum class OutputFormat {
143#ifdef PDF_ENABLE_SKIA
154 bool show_config =
false;
155 bool show_metadata =
false;
156 bool send_events =
false;
157 bool use_load_mem_document =
false;
158 bool render_oneshot =
false;
159 bool lcd_text =
false;
160 bool no_nativetext =
false;
161 bool grayscale =
false;
162 bool forced_color =
false;
163 bool fill_to_stroke =
false;
164 bool limit_cache =
false;
165 bool force_halftone =
false;
166 bool printing =
false;
167 bool no_smoothtext =
false;
168 bool no_smoothimage =
false;
169 bool no_smoothpath =
false;
170 bool reverse_byte_order =
false;
171 bool save_attachments =
false;
172 bool save_images =
false;
173 bool save_rendered_images =
false;
174 bool save_thumbnails =
false;
175 bool save_thumbnails_decoded =
false;
176 bool save_thumbnails_raw =
false;
177 RendererType use_renderer_type = RendererType::kDefault;
179 bool disable_javascript =
false;
180 std::string js_flags;
182 bool disable_xfa =
false;
187#ifdef ENABLE_CALLGRIND
188 bool callgrind_delimiters =
false;
190#if defined(__APPLE__) || (defined(__linux__
) && !defined(__ANDROID__))
191 bool linux_no_system_fonts =
false;
193 bool croscore_font_names =
false;
194 OutputFormat output_format = OutputFormat::kNone;
195 std::string password;
196 std::string scale_factor_as_string;
197 std::string exe_path;
198 std::string bin_directory;
199 std::string font_directory;
205int PageRenderFlagsFromOptions(
const Options& options) {
207 if (options.lcd_text)
209 if (options.no_nativetext)
211 if (options.grayscale)
213 if (options.fill_to_stroke)
215 if (options.limit_cache)
217 if (options.force_halftone)
219 if (options.printing)
221 if (options.no_smoothtext)
223 if (options.no_smoothimage)
225 if (options.no_smoothpath)
227 if (options.reverse_byte_order)
232absl::optional<std::string> ExpandDirectoryPath(
const std::string& path) {
233#if defined(WORDEXP_AVAILABLE
)
235 if (wordexp(path.c_str(), &expansion, 0) != 0 || expansion.we_wordc < 1) {
236 wordfree(&expansion);
241 absl::optional<std::string> ret_val = {expansion.we_wordv[0]};
242 wordfree(&expansion);
249absl::optional<
const char*> GetCustomFontPath(
const Options& options) {
250#if defined(__APPLE__) || (defined(__linux__
) && !defined(__ANDROID__))
253 if (options.linux_no_system_fonts)
258 if (options.font_directory.empty())
259 return absl::nullopt;
262 return options.font_directory.c_str();
265struct FPDF_FORMFILLINFO_PDFiumTest
final :
public FPDF_FORMFILLINFO {
268 std::map<
int, ScopedFPDFPage> loaded_pages;
272 FPDF_FORMHANDLE form_handle;
275FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
276 FPDF_FORMFILLINFO* form_fill_info) {
277 return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info);
280void OutputMD5Hash(
const char* file_name, pdfium::span<
const uint8_t> output) {
282 std::string hash = GenerateMD5Base16(output);
283 printf(
"MD5:%s:%s\n", file_name, hash.c_str());
288struct V8IsolateDeleter {
289 inline void operator()(v8::Isolate* ptr) { ptr->Dispose(); }
295int ExampleAppAlert(IPDF_JSPLATFORM*,
297 FPDF_WIDESTRING title,
300 printf(
"%ls", GetPlatformWString(title).c_str());
302 printf(
"[icon=%d,type=%d]", icon, type);
303 printf(
": %ls\n", GetPlatformWString(msg).c_str());
307void ExampleAppBeep(IPDF_JSPLATFORM*,
int type) {
308 printf(
"BEEP!!! %d\n", type);
311int ExampleAppResponse(IPDF_JSPLATFORM*,
312 FPDF_WIDESTRING question,
313 FPDF_WIDESTRING title,
314 FPDF_WIDESTRING default_value,
315 FPDF_WIDESTRING label,
316 FPDF_BOOL is_password,
319 printf(
"%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
320 GetPlatformWString(title).c_str(),
321 GetPlatformWString(question).c_str(),
322 GetPlatformWString(default_value).c_str(),
323 GetPlatformWString(label).c_str(), is_password, length);
326 auto* ptr =
static_cast<uint8_t*>(response);
334int ExampleDocGetFilePath(IPDF_JSPLATFORM*,
void* file_path,
int length) {
335 static const char kPath[] =
"myfile.pdf";
336 constexpr int kRequired =
static_cast<
int>(
sizeof(kPath));
337 if (file_path && length >= kRequired)
338 memcpy(file_path, kPath, kRequired);
342void ExampleDocMail(IPDF_JSPLATFORM*,
347 FPDF_WIDESTRING Subject,
350 FPDF_WIDESTRING Msg) {
351 printf(
"Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", UI,
352 GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
353 GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
354 GetPlatformWString(Msg).c_str());
357void ExampleDocPrint(IPDF_JSPLATFORM*,
362 FPDF_BOOL bShrinkToFit,
363 FPDF_BOOL bPrintAsImage,
365 FPDF_BOOL bAnnotations) {
366 printf(
"Doc Print: %d, %d, %d, %d, %d, %d, %d, %d\n", bUI, nStart, nEnd,
367 bSilent, bShrinkToFit, bPrintAsImage, bReverse, bAnnotations);
370void ExampleDocSubmitForm(IPDF_JSPLATFORM*,
373 FPDF_WIDESTRING url) {
374 printf(
"Doc Submit Form: url=%ls + %d data bytes:\n",
375 GetPlatformWString(url).c_str(), length);
376 uint8_t* ptr =
reinterpret_cast<uint8_t*>(formData);
377 for (
int i = 0; i < length; ++i)
378 printf(
" %02x", ptr[i]);
382void ExampleDocGotoPage(IPDF_JSPLATFORM*,
int page_number) {
383 printf(
"Goto Page: %d\n", page_number);
386int ExampleFieldBrowse(IPDF_JSPLATFORM*,
void* file_path,
int length) {
387 static const char kPath[] =
"selected.txt";
388 constexpr int kRequired =
static_cast<
int>(
sizeof(kPath));
389 if (file_path && length >= kRequired)
390 memcpy(file_path, kPath, kRequired);
396FPDF_BOOL ExamplePopupMenu(FPDF_FORMFILLINFO* pInfo,
398 FPDF_WIDGET always_null,
402 printf(
"Popup: x=%2.1f, y=%2.1f, flags=0x%x\n", x, y, flags);
407void ExampleNamedAction(FPDF_FORMFILLINFO* pInfo, FPDF_BYTESTRING name) {
408 printf(
"Execute named action: %s\n", name);
411void ExampleUnsupportedHandler(UNSUPPORT_INFO*,
int type) {
412 std::string feature =
"Unknown";
418 feature =
"Portfolios_Packages";
422 feature =
"Attachment";
425 feature =
"Rights_Management";
428 feature =
"Shared_Review";
433 feature =
"Shared_Form";
449 feature =
"Digital_Signature";
452 printf(
"Unsupported feature: %s.\n", feature.c_str());
455bool ParseCommandLine(
const std::vector<std::string>& args,
457 std::vector<std::string>* files) {
461 options->exe_path = args[0];
464 for (; cur_idx < args.size(); ++cur_idx) {
465 const std::string& cur_arg = args[cur_idx];
466 if (cur_arg ==
"--show-config") {
467 options->show_config =
true;
468 }
else if (cur_arg ==
"--show-metadata") {
469 options->show_metadata =
true;
470 }
else if (cur_arg ==
"--send-events") {
471 options->send_events =
true;
472 }
else if (cur_arg ==
"--mem-document") {
473 options->use_load_mem_document =
true;
474 }
else if (cur_arg ==
"--render-oneshot") {
475 options->render_oneshot =
true;
476 }
else if (cur_arg ==
"--lcd-text") {
477 options->lcd_text =
true;
478 }
else if (cur_arg ==
"--no-nativetext") {
479 options->no_nativetext =
true;
480 }
else if (cur_arg ==
"--grayscale") {
481 options->grayscale =
true;
482 }
else if (cur_arg ==
"--forced-color") {
483 options->forced_color =
true;
484 }
else if (cur_arg ==
"--fill-to-stroke") {
485 options->fill_to_stroke =
true;
486 }
else if (cur_arg ==
"--limit-cache") {
487 options->limit_cache =
true;
488 }
else if (cur_arg ==
"--force-halftone") {
489 options->force_halftone =
true;
490 }
else if (cur_arg ==
"--printing") {
491 options->printing =
true;
492 }
else if (cur_arg ==
"--no-smoothtext") {
493 options->no_smoothtext =
true;
494 }
else if (cur_arg ==
"--no-smoothimage") {
495 options->no_smoothimage =
true;
496 }
else if (cur_arg ==
"--no-smoothpath") {
497 options->no_smoothpath =
true;
498 }
else if (cur_arg ==
"--reverse-byte-order") {
499 options->reverse_byte_order =
true;
500 }
else if (cur_arg ==
"--save-attachments") {
501 options->save_attachments =
true;
502 }
else if (cur_arg ==
"--save-images") {
503 if (options->save_rendered_images) {
505 "--save-rendered-images conflicts with --save-images\n");
508 options->save_images =
true;
509 }
else if (cur_arg ==
"--save-rendered-images") {
510 if (options->save_images) {
512 "--save-images conflicts with --save-rendered-images\n");
515 options->save_rendered_images =
true;
516 }
else if (cur_arg ==
"--save-thumbs") {
517 options->save_thumbnails =
true;
518 }
else if (cur_arg ==
"--save-thumbs-dec") {
519 options->save_thumbnails_decoded =
true;
520 }
else if (cur_arg ==
"--save-thumbs-raw") {
521 options->save_thumbnails_raw =
true;
522 }
else if (ParseSwitchKeyValue(cur_arg,
"--use-renderer=", &value)) {
523 if (options->use_renderer_type != RendererType::kDefault) {
524 fprintf(stderr,
"Duplicate --use-renderer argument\n");
527 if (value ==
"agg") {
528 options->use_renderer_type = RendererType::kAgg;
530 }
else if (value ==
"gdi") {
531 options->use_renderer_type = RendererType::kGdi;
533#if defined(PDF_ENABLE_SKIA)
534 }
else if (value ==
"skia") {
535 options->use_renderer_type = RendererType::kSkia;
538 fprintf(stderr,
"Invalid --use-renderer argument\n");
542 }
else if (cur_arg ==
"--disable-javascript") {
543 options->disable_javascript =
true;
544 }
else if (ParseSwitchKeyValue(cur_arg,
"--js-flags=", &value)) {
545 if (!options->js_flags.empty()) {
546 fprintf(stderr,
"Duplicate --js-flags argument\n");
549 options->js_flags = value;
551 }
else if (cur_arg ==
"--disable-xfa") {
552 options->disable_xfa =
true;
555#ifdef ENABLE_CALLGRIND
556 }
else if (cur_arg ==
"--callgrind-delim") {
557 options->callgrind_delimiters =
true;
559#if defined(__APPLE__) || (defined(__linux__
) && !defined(__ANDROID__))
560 }
else if (cur_arg ==
"--no-system-fonts") {
561 options->linux_no_system_fonts =
true;
563 }
else if (cur_arg ==
"--croscore-font-names") {
564 options->croscore_font_names =
true;
565 }
else if (cur_arg ==
"--ppm") {
566 if (options->output_format != OutputFormat::kNone) {
567 fprintf(stderr,
"Duplicate or conflicting --ppm argument\n");
570 options->output_format = OutputFormat::kPpm;
571 }
else if (cur_arg ==
"--png") {
572 if (options->output_format != OutputFormat::kNone) {
573 fprintf(stderr,
"Duplicate or conflicting --png argument\n");
576 options->output_format = OutputFormat::kPng;
577 }
else if (cur_arg ==
"--txt") {
578 if (options->output_format != OutputFormat::kNone) {
579 fprintf(stderr,
"Duplicate or conflicting --txt argument\n");
582 options->output_format = OutputFormat::kText;
583 }
else if (cur_arg ==
"--annot") {
584 if (options->output_format != OutputFormat::kNone) {
585 fprintf(stderr,
"Duplicate or conflicting --annot argument\n");
588 options->output_format = OutputFormat::kAnnot;
589#ifdef PDF_ENABLE_SKIA
590 }
else if (cur_arg ==
"--skp") {
591 if (options->output_format != OutputFormat::kNone) {
592 fprintf(stderr,
"Duplicate or conflicting --skp argument\n");
595 options->output_format = OutputFormat::kSkp;
597 }
else if (cur_arg ==
"--xps") {
598 if (options->output_format != OutputFormat::kNone) {
599 fprintf(stderr,
"Duplicate or conflicting --xps argument\n");
602 options->output_format = OutputFormat::kXps;
605 }
else if (ParseSwitchKeyValue(cur_arg,
"--font-dir=", &value)) {
606 if (!options->font_directory.empty()) {
607 fprintf(stderr,
"Duplicate --font-dir argument\n");
610 std::string path = value;
611 absl::optional<std::string> expanded_path = ExpandDirectoryPath(path);
612 if (!expanded_path.has_value()) {
613 fprintf(stderr,
"Failed to expand --font-dir, %s\n", path.c_str());
617 if (!PathService::DirectoryExists(expanded_path.value())) {
618 fprintf(stderr,
"--font-dir, %s, appears to not be a directory\n",
623 options->font_directory = expanded_path.value();
626 }
else if (cur_arg ==
"--emf") {
627 if (options->output_format != OutputFormat::kNone) {
628 fprintf(stderr,
"Duplicate or conflicting --emf argument\n");
631 options->output_format = OutputFormat::kEmf;
632 }
else if (cur_arg ==
"--ps2") {
633 if (options->output_format != OutputFormat::kNone) {
634 fprintf(stderr,
"Duplicate or conflicting --ps2 argument\n");
637 options->output_format = OutputFormat::kPs2;
638 }
else if (cur_arg ==
"--ps3") {
639 if (options->output_format != OutputFormat::kNone) {
640 fprintf(stderr,
"Duplicate or conflicting --ps3 argument\n");
643 options->output_format = OutputFormat::kPs3;
644 }
else if (cur_arg ==
"--ps3-type42") {
645 if (options->output_format != OutputFormat::kNone) {
646 fprintf(stderr,
"Duplicate or conflicting --ps3-type42 argument\n");
649 options->output_format = OutputFormat::kPs3Type42;
650 }
else if (cur_arg ==
"--bmp") {
651 if (options->output_format != OutputFormat::kNone) {
652 fprintf(stderr,
"Duplicate or conflicting --bmp argument\n");
655 options->output_format = OutputFormat::kBmp;
659#ifdef V8_USE_EXTERNAL_STARTUP_DATA
660 }
else if (ParseSwitchKeyValue(cur_arg,
"--bin-dir=", &value)) {
661 if (!options->bin_directory.empty()) {
662 fprintf(stderr,
"Duplicate --bin-dir argument\n");
665 std::string path = value;
666 absl::optional<std::string> expanded_path = ExpandDirectoryPath(path);
667 if (!expanded_path.has_value()) {
668 fprintf(stderr,
"Failed to expand --bin-dir, %s\n", path.c_str());
671 options->bin_directory = expanded_path.value();
675 }
else if (ParseSwitchKeyValue(cur_arg,
"--password=", &value)) {
676 if (!options->password.empty()) {
677 fprintf(stderr,
"Duplicate --password argument\n");
680 options->password = value;
681 }
else if (ParseSwitchKeyValue(cur_arg,
"--scale=", &value)) {
682 if (!options->scale_factor_as_string.empty()) {
683 fprintf(stderr,
"Duplicate --scale argument\n");
686 options->scale_factor_as_string = value;
687 }
else if (cur_arg ==
"--show-pageinfo") {
688 if (options->output_format != OutputFormat::kNone) {
689 fprintf(stderr,
"Duplicate or conflicting --show-pageinfo argument\n");
692 options->output_format = OutputFormat::kPageInfo;
693 }
else if (cur_arg ==
"--show-structure") {
694 if (options->output_format != OutputFormat::kNone) {
695 fprintf(stderr,
"Duplicate or conflicting --show-structure argument\n");
698 options->output_format = OutputFormat::kStructure;
699 }
else if (ParseSwitchKeyValue(cur_arg,
"--pages=", &value)) {
700 if (options->pages) {
701 fprintf(stderr,
"Duplicate --pages argument\n");
704 options->pages =
true;
705 const std::string pages_string = value;
706 size_t first_dash = pages_string.find(
'-');
707 if (first_dash == std::string::npos) {
708 std::stringstream(pages_string) >> options->first_page;
709 options->last_page = options->first_page;
711 std::stringstream(pages_string.substr(0, first_dash)) >>
713 std::stringstream(pages_string.substr(first_dash + 1)) >>
716 }
else if (cur_arg ==
"--md5") {
718 }
else if (ParseSwitchKeyValue(cur_arg,
"--time=", &value)) {
719 if (options->time > -1) {
720 fprintf(stderr,
"Duplicate --time argument\n");
723 const std::string time_string = value;
724 std::stringstream(time_string) >> options->time;
725 if (options->time < 0) {
726 fprintf(stderr,
"Invalid --time argument, must be non-negative\n");
729 }
else if (cur_arg.size() >= 2 && cur_arg[0] ==
'-' && cur_arg[1] ==
'-') {
730 fprintf(stderr,
"Unrecognized argument %s\n", cur_arg.c_str());
736 for (size_t i = cur_idx; i < args.size(); i++)
737 files->push_back(args[i]);
742void PrintLastError() {
744 fprintf(stderr,
"Load pdf docs unsuccessful: ");
747 fprintf(stderr,
"Success");
750 fprintf(stderr,
"Unknown error");
753 fprintf(stderr,
"File not found or could not be opened");
756 fprintf(stderr,
"File not in PDF format or corrupted");
759 fprintf(stderr,
"Password required or incorrect password");
762 fprintf(stderr,
"Unsupported security scheme");
765 fprintf(stderr,
"Page not found or content error");
768 fprintf(stderr,
"Unknown error %ld", err);
770 fprintf(stderr,
".\n");
773FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* avail, size_t offset, size_t size) {
777void Add_Segment(FX_DOWNLOADHINTS* hints, size_t offset, size_t size) {}
779FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
782 FPDF_FORMFILLINFO_PDFiumTest* form_fill_info =
783 ToPDFiumTestFormFillInfo(param);
784 auto& loaded_pages = form_fill_info->loaded_pages;
785 auto iter = loaded_pages.find(index);
786 if (iter != loaded_pages.end())
787 return iter->second.get();
789 ScopedFPDFPage page(FPDF_LoadPage(doc, index));
794 FPDF_PAGE page_ptr = page.get();
795 loaded_pages[index] =
std::move(page);
797 FPDF_FORMHANDLE& form_handle = form_fill_info->form_handle;
806FPDF_BOOL NeedToPauseNow(IFSDK_PAUSE* p) {
810class Processor
final {
812 Processor(
const Options* options,
const std::function<
void()>* idler)
813 : options_(options), idler_(idler) {
818 const Options& options()
const {
return *options_; }
819 const std::function<
void()>& idler()
const {
return *idler_; }
822 ComFactory& com_factory() {
return com_factory_; }
826 void Idle()
const { idler()(); }
828 void ProcessPdf(
const std::string& name,
829 pdfium::span<
const uint8_t> data,
830 const std::string& events);
833 const Options* options_;
834 const std::function<
void()>* idler_;
837 ComFactory com_factory_;
841class PdfProcessor
final {
843 PdfProcessor(Processor* processor,
844 const std::string* name,
845 const std::string* events,
847 FPDF_FORMHANDLE form,
848 FPDF_FORMFILLINFO_PDFiumTest* form_fill_info)
849 : processor_(processor),
854 form_fill_info_(form_fill_info) {
860 DCHECK(form_fill_info_);
863 bool ProcessPage(
int page_index);
867 const Options& options()
const {
return processor_->options(); }
868 const std::function<
void()>& idler()
const {
return processor_->idler(); }
871 ComFactory& com_factory() {
return processor_->com_factory(); }
875 const std::string& name()
const {
return *name_; }
876 const std::string& events()
const {
return *events_; }
877 FPDF_DOCUMENT doc()
const {
return doc_; }
878 FPDF_FORMHANDLE form()
const {
return form_; }
881 void Idle()
const { idler()(); }
883 FPDF_PAGE GetPage(
int page_index)
const {
884 return GetPageForIndex(form_fill_info_, doc_, page_index);
887 Processor* processor_;
888 const std::string* name_;
889 const std::string* events_;
891 FPDF_FORMHANDLE form_;
892 FPDF_FORMFILLINFO_PDFiumTest* form_fill_info_;
902 using PageWriter =
std::function<
bool(BitmapPageRenderer& renderer,
903 const std::string& name,
908 static PageWriter WrapPageWriter(
909 void (*text_page_writer)(FPDF_TEXTPAGE text_page,
910 const char* pdf_name,
912 return [text_page_writer](BitmapPageRenderer& renderer,
913 const std::string& name,
int page_index,
915 ScopedFPDFTextPage text_page(FPDFText_LoadPage(renderer.page()));
920 text_page_writer(text_page.get(), name.c_str(), page_index);
926 static PageWriter WrapPageWriter(
void (*page_writer)(FPDF_PAGE page,
927 const char* pdf_name,
929 return [page_writer](BitmapPageRenderer& renderer,
const std::string& name,
930 int page_index,
bool ) {
931 page_writer(renderer
.page(), name.c_str(), page_index);
938 static PageWriter WrapPageWriter(
939 std::string (*bitmap_writer)(
const char* pdf_name,
945 return [bitmap_writer](BitmapPageRenderer& renderer,
946 const std::string& name,
int page_index,
bool md5) {
949 std::string image_file_name = bitmap_writer(
950 name.c_str(), page_index, buffer, stride,
952 if (image_file_name.empty()) {
958 OutputMD5Hash(image_file_name.c_str(),
959 {
static_cast<
const uint8_t*>(buffer),
960 static_cast<size_t>(stride) * renderer
.height()});
966 bool HasOutput()
const override {
return !!bitmap_; }
968 void Finish(FPDF_FORMHANDLE form)
override {
969 FPDF_FFLDraw(form, bitmap(),
page(), 0, 0,
975 bool Write(
const std::string& name,
int page_index,
bool md5)
override {
976 return writer_ && writer_(*
this, name, page_index, md5);
980 BitmapPageRenderer(FPDF_PAGE page,
984 const std::function<
void()>& idler,
988 writer_(std::move(writer)) {}
990 bool InitializeBitmap(
void* first_scan) {
991 bool alpha = FPDFPage_HasTransparency(
page());
992 bitmap_.reset(FPDFBitmap_CreateEx(
995 width() *
sizeof(uint32_t)));
1000 FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
1001 FPDFBitmap_FillRect(bitmap(), 0, 0,
width(),
1006 void ResetBitmap() { bitmap_.reset(); }
1008 void Idle()
const { idler_(); }
1009 FPDF_BITMAP bitmap() {
return bitmap_.get(); }
1012 const std::function<
void()>& idler_;
1014 ScopedFPDFBitmap bitmap_;
1018class OneShotBitmapPageRenderer :
public BitmapPageRenderer {
1020 OneShotBitmapPageRenderer(FPDF_PAGE page,
1024 const std::function<
void()>& idler,
1026 : BitmapPageRenderer(page,
1031 std::move(writer)) {}
1033 bool Start()
override {
1034 if (!InitializeBitmap(
nullptr)) {
1041 FPDF_RenderPageBitmap(bitmap(),
page(), 0, 0,
1049class ProgressiveBitmapPageRenderer :
public BitmapPageRenderer {
1051 ProgressiveBitmapPageRenderer(FPDF_PAGE page,
1055 const std::function<
void()>& idler,
1057 const FPDF_COLORSCHEME* color_scheme)
1058 : BitmapPageRenderer(page,
1064 color_scheme_(color_scheme) {
1066 pause_.NeedToPauseNow = &NeedToPauseNow;
1069 bool Start()
override {
1070 if (!InitializeBitmap(
nullptr)) {
1074 if (FPDF_RenderPageBitmapWithColorScheme_Start(
1078 to_be_continued_ =
true;
1083 bool Continue()
override {
1084 if (to_be_continued_) {
1085 to_be_continued_ = (FPDF_RenderPage_Continue(
page(), &pause_) ==
1088 return to_be_continued_;
1091 void Finish(FPDF_FORMHANDLE form)
override {
1092 BitmapPageRenderer::Finish(form);
1093 FPDF_RenderPage_Close(
page());
1098 const FPDF_COLORSCHEME* color_scheme_;
1100 bool to_be_continued_ =
false;
1104class ScopedGdiDc final {
1106 ~ScopedGdiDc() { Reset(
nullptr); }
1108 void Reset(HDC dc) {
1110 [[maybe_unused]] BOOL success = DeleteDC(dc_);
1116 HDC Get()
const {
return dc_; }
1122class ScopedGdiObject final {
1124 ~ScopedGdiObject() { Reset(
nullptr); }
1126 void Reset(HGDIOBJ object) {
1128 [[maybe_unused]] BOOL success = DeleteObject(object_);
1134 HGDIOBJ Get()
const {
return object_; }
1137 HGDIOBJ object_ =
nullptr;
1140class GdiDisplayPageRenderer :
public BitmapPageRenderer {
1142 GdiDisplayPageRenderer(FPDF_PAGE page,
1146 const std::function<
void()>& idler,
1148 : BitmapPageRenderer(page,
1153 std::move(writer)) {}
1155 ~GdiDisplayPageRenderer() override {
1160 bool Start() override {
1162 dc_.Reset(CreateCompatibleDC(
nullptr));
1168 BITMAPINFO dib_info;
1169 memset(&dib_info, 0,
sizeof(BITMAPINFO));
1170 dib_info.bmiHeader.biSize =
sizeof(BITMAPINFOHEADER);
1171 dib_info.bmiHeader.biWidth = width();
1172 dib_info.bmiHeader.biHeight = -height();
1173 dib_info.bmiHeader.biPlanes = 1;
1174 dib_info.bmiHeader.biBitCount = 32;
1175 dib_info.bmiHeader.biCompression = BI_RGB;
1178 dib_.Reset(CreateDIBSection(dc_.Get(), &dib_info, DIB_RGB_COLORS,
1179 &dib_pixels,
nullptr,
1181 if (!dib_.Get() || !InitializeBitmap(dib_pixels)) {
1184 pdfium::base::win::ScopedSelectObject select_dib(dc_.Get(), dib_.Get());
1187 FPDF_RenderPage(dc_.Get(), page(), 0, 0,
1188 width(), height(), 0,
1191 return !!GdiFlush();
1194 void Finish(FPDF_FORMHANDLE ) override {
1199 const int stride = FPDFBitmap_GetStride(bitmap());
1200 DCHECK_EQ(width() *
sizeof(uint32_t),
static_cast<size_t>(stride));
1201 const int pixel_stride = stride /
sizeof(uint32_t);
1203 uint32_t* scanline =
1204 reinterpret_cast<uint32_t*>(FPDFBitmap_GetBuffer(bitmap()));
1205 for (
int row = 0; row < height(); ++row) {
1206 for (
int column = 0; column < width(); ++column) {
1207 scanline[column] |= 0xFF000000;
1209 scanline += pixel_stride;
1215 ScopedGdiObject dib_;
1219#ifdef PDF_ENABLE_SKIA
1220class SkCanvasPageRenderer :
public PageRenderer {
1222 bool Start() override {
1223 FPDF_RenderPageSkia(
reinterpret_cast<FPDF_SKIA_CANVAS>(canvas()), page(),
1228 void Finish(FPDF_FORMHANDLE form) override {
1229 FPDF_FFLDrawSkia(form,
reinterpret_cast<FPDF_SKIA_CANVAS>(canvas()), page(),
1230 0, 0, width(), height(),
1235 SkCanvasPageRenderer(FPDF_PAGE page,
int width,
int height,
int flags)
1236 : PageRenderer(page, width, height, flags) {}
1238 virtual SkCanvas* canvas() = 0;
1241class SkPicturePageRenderer final :
public SkCanvasPageRenderer {
1243 SkPicturePageRenderer(FPDF_PAGE page,
int width,
int height,
int flags)
1244 : SkCanvasPageRenderer(page, width, height, flags) {}
1246 bool HasOutput()
const override {
return !!picture_; }
1248 bool Start() override {
1249 recorder_ = std::make_unique<SkPictureRecorder>();
1250 recorder_->beginRecording(width(), height());
1251 return SkCanvasPageRenderer::Start();
1254 void Finish(FPDF_FORMHANDLE form) override {
1255 SkCanvasPageRenderer::Finish(form);
1256 picture_ = recorder_->finishRecordingAsPicture();
1260 bool Write(
const std::string& name,
int page_index,
bool md5) override {
1261 std::string image_file_name = WriteSkp(name.c_str(), page_index, *picture_);
1262 if (image_file_name.empty())
1267 sk_sp<SkSurface> surface = SkSurfaces::Raster(SkImageInfo::MakeN32Premul(
1268 width(), height()));
1273 surface->getCanvas()->clear(SK_ColorWHITE);
1274 surface->getCanvas()->drawPicture(picture_);
1278 if (!surface->peekPixels(&pixmap))
1281 OutputMD5Hash(image_file_name.c_str(),
1282 {
static_cast<
const uint8_t*>(pixmap.addr()),
1283 pixmap.computeByteSize()});
1289 SkCanvas* canvas() override {
return recorder_->getRecordingCanvas(); }
1292 std::unique_ptr<SkPictureRecorder> recorder_;
1293 sk_sp<SkPicture> picture_;
1296class SkDocumentPageRenderer final :
public SkCanvasPageRenderer {
1298 SkDocumentPageRenderer(std::unique_ptr<SkWStream> stream,
1299 sk_sp<SkDocument> document,
1304 : SkCanvasPageRenderer(page, width, height, flags),
1305 stream_(std::move(stream)),
1306 document_(std::move(document)) {
1311 bool HasOutput()
const override {
return has_output_; }
1313 bool Start() override {
1319 canvas_ = document_->beginPage(width(), height());
1324 return SkCanvasPageRenderer::Start();
1327 void Finish(FPDF_FORMHANDLE form) override {
1328 SkCanvasPageRenderer::Finish(form);
1332 document_->endPage();
1337 bool Write(
const std::string& ,
1340 bool success = HasOutput();
1353 SkCanvas* canvas() override {
return canvas_; }
1356 std::unique_ptr<SkWStream> stream_;
1357 sk_sp<SkDocument> document_;
1359 SkCanvas* canvas_ =
nullptr;
1360 bool has_output_ =
false;
1364bool PdfProcessor::ProcessPage(
const int page_index) {
1365 FPDF_PAGE page = GetPage(page_index);
1370 if (options().send_events) {
1373 if (options().save_images) {
1374 WriteImages(page, name().c_str(), page_index);
1376 if (options().save_rendered_images) {
1377 WriteRenderedImages(doc(), page, name().c_str(), page_index);
1379 if (options().save_thumbnails) {
1380 WriteThumbnail(page, name().c_str(), page_index);
1382 if (options().save_thumbnails_decoded) {
1383 WriteDecodedThumbnailStream(page, name().c_str(), page_index);
1385 if (options().save_thumbnails_raw) {
1386 WriteRawThumbnailStream(page, name().c_str(), page_index);
1388 if (options().output_format == OutputFormat::kPageInfo) {
1392 if (options().output_format == OutputFormat::kStructure) {
1397 ScopedFPDFTextPage text_page(FPDFText_LoadPage(page));
1399 if (!options().scale_factor_as_string.empty()) {
1400 std::stringstream(options().scale_factor_as_string) >> scale;
1405 int flags = PageRenderFlagsFromOptions(options());
1407 std::unique_ptr<PageRenderer> renderer;
1408 BitmapPageRenderer::PageWriter writer;
1409 switch (options().output_format) {
1410 case OutputFormat::kText:
1411 writer = BitmapPageRenderer::WrapPageWriter(
WriteText);
1414 case OutputFormat::kAnnot:
1415 writer = BitmapPageRenderer::WrapPageWriter(
WriteAnnot);
1418 case OutputFormat::kPpm:
1419 writer = BitmapPageRenderer::WrapPageWriter(
WritePpm);
1422 case OutputFormat::kPng:
1423 writer = BitmapPageRenderer::WrapPageWriter(
WritePng);
1427 case OutputFormat::kBmp:
1428 writer = BitmapPageRenderer::WrapPageWriter(WriteBmp);
1431 case OutputFormat::kEmf:
1433 writer = BitmapPageRenderer::WrapPageWriter(WriteEmf);
1436 case OutputFormat::kPs2:
1437 case OutputFormat::kPs3:
1439 writer = BitmapPageRenderer::WrapPageWriter(WritePS);
1443#ifdef PDF_ENABLE_SKIA
1444 case OutputFormat::kSkp:
1445 renderer = std::make_unique<SkPicturePageRenderer>(
1446 page, width, height, flags);
1450 case OutputFormat::kXps: {
1451 IXpsOMObjectFactory* xps_factory = com_factory().GetXpsOMObjectFactory();
1456 std::unique_ptr<SkWStream> stream =
1457 WriteToSkWStream(name(), page_index,
"xps");
1462 sk_sp<SkDocument> document =
1463 SkXPS::MakeDocument(stream.get(), xps_factory);
1468 renderer = std::make_unique<SkDocumentPageRenderer>(
1469 std::move(stream), std::move(document), page, width, height, flags);
1481 if (!renderer && options().use_renderer_type == RendererType::kGdi) {
1482 renderer = std::make_unique<GdiDisplayPageRenderer>(
1483 page, width, height, flags, idler(),
1490 if (options().render_oneshot) {
1491 renderer =
std::make_unique<OneShotBitmapPageRenderer>(
1492 page, width, height, flags, idler(),
1498 FPDF_COLORSCHEME color_scheme;
1499 color_scheme.path_fill_color = 0xFFFF0000;
1500 color_scheme.path_stroke_color = 0xFF00FF00;
1501 color_scheme.text_fill_color = 0xFF0000FF;
1502 color_scheme.text_stroke_color = 0xFF00FFFF;
1504 renderer =
std::make_unique<ProgressiveBitmapPageRenderer>(
1505 page, width, height, flags, idler(),
1506 std::move(writer), options().forced_color ? &color_scheme :
nullptr);
1510 if (renderer->Start()) {
1511 while (renderer->Continue())
1513 renderer->Finish(form());
1514 renderer->Write(name(), page_index, options().md5);
1516 fprintf(stderr,
"Page was too large to be rendered.\n");
1525 return renderer->HasOutput();
1528void Processor::ProcessPdf(
const std::string& name,
1529 pdfium::span<
const uint8_t> data,
1530 const std::string& events) {
1533 FPDF_FILEACCESS file_access = {};
1534 file_access.m_FileLen =
static_cast<
unsigned long>(data.size());
1536 file_access.m_Param = &loader;
1538 FX_FILEAVAIL file_avail = {};
1540 file_avail.IsDataAvail = Is_Data_Avail;
1542 FX_DOWNLOADHINTS hints = {};
1547 ScopedFPDFAvail pdf_avail(FPDFAvail_Create(&file_avail, &file_access));
1550 ScopedFPDFDocument doc;
1552 const char* password =
1553 options().password.empty() ?
nullptr : options().password.c_str();
1554 bool is_linearized =
false;
1555 if (options().use_load_mem_document) {
1556 doc.reset(FPDF_LoadMemDocument(data.data(), data.size(), password));
1560 doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), password));
1563 avail_status = FPDFAvail_IsDocAvail(pdf_avail.get(), &hints);
1566 fprintf(stderr,
"Unknown error in checking if doc was available.\n");
1569 avail_status = FPDFAvail_IsFormAvail(pdf_avail.get(), &hints);
1573 "Error %d was returned in checking if form was available.\n",
1577 is_linearized =
true;
1589 if (!FPDF_DocumentHasValidCrossReferenceTable(doc.get()))
1590 fprintf(stderr,
"Document has invalid cross reference table\n");
1592 if (options().show_metadata) {
1593 DumpMetaData(doc.get());
1596 if (options().save_attachments) {
1597 WriteAttachments(doc.get(), name);
1601 IPDF_JSPLATFORM platform_callbacks = {};
1602 platform_callbacks.version = 3;
1603 platform_callbacks.app_alert = ExampleAppAlert;
1604 platform_callbacks.app_beep = ExampleAppBeep;
1605 platform_callbacks.app_response = ExampleAppResponse;
1606 platform_callbacks.Doc_getFilePath = ExampleDocGetFilePath;
1607 platform_callbacks.Doc_mail = ExampleDocMail;
1608 platform_callbacks.Doc_print = ExampleDocPrint;
1609 platform_callbacks.Doc_submitForm = ExampleDocSubmitForm;
1610 platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
1611 platform_callbacks.Field_browse = ExampleFieldBrowse;
1614 FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {};
1615#ifdef PDF_ENABLE_XFA
1616 form_callbacks.version = 2;
1617 form_callbacks.xfa_disabled =
1618 options().disable_xfa || options().disable_javascript;
1619 form_callbacks.FFI_PopupMenu = ExamplePopupMenu;
1624 form_callbacks.FFI_GetPage = GetPageForIndex;
1627 if (!options().disable_javascript) {
1628 form_callbacks.m_pJsPlatform = &platform_callbacks;
1632 ScopedFPDFFormHandle form(
1633 FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks));
1634 form_callbacks.form_handle = form.get();
1636#ifdef PDF_ENABLE_XFA
1637 if (!options().disable_xfa && !options().disable_javascript) {
1638 int doc_type = FPDF_GetFormType(doc.get());
1639 if (doc_type == FORMTYPE_XFA_FULL || doc_type == FORMTYPE_XFA_FOREGROUND) {
1640 if (!FPDF_LoadXFA(doc.get()))
1641 fprintf(stderr,
"LoadXFA unsuccessful, continuing anyway.\n");
1647 FPDF_SetFormFieldHighlightAlpha(form.get(), 100);
1648 FORM_DoDocumentJSAction(form.get());
1649 FORM_DoDocumentOpenAction(form.get());
1652 if (options().output_format == OutputFormat::kPs2) {
1653 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT2);
1654 }
else if (options().output_format == OutputFormat::kPs3) {
1655 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT3);
1656 }
else if (options().output_format == OutputFormat::kPs3Type42) {
1657 FPDF_SetPrintMode(FPDF_PRINTMODE_POSTSCRIPT3_TYPE42);
1661 int page_count = FPDF_GetPageCount(doc.get());
1662 int processed_pages = 0;
1664 int first_page = options().pages ? options().first_page : 0;
1665 int last_page = options().pages ? options().last_page + 1 : page_count;
1666 PdfProcessor pdf_processor(
this, &name, &events, doc.get(), form.get(),
1668 for (
int i = first_page; i < last_page; ++i) {
1669 if (is_linearized) {
1672 avail_status = FPDFAvail_IsPageAvail(pdf_avail.get(), i, &hints);
1675 fprintf(stderr,
"Unknown error in checking if page %d is available.\n",
1680 if (pdf_processor.ProcessPage(i)) {
1691 fprintf(stderr,
"Processed %d pages.\n", processed_pages);
1693 fprintf(stderr,
"Skipped %d bad pages.\n", bad_pages);
1698 [[maybe_unused]]
auto append_config = [&config](
const char* name) {
1699 if (!config.empty())
1705 append_config(
"V8");
1707#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1708 append_config(
"V8_EXTERNAL");
1710#ifdef PDF_ENABLE_XFA
1711 append_config(
"XFA");
1713#ifdef PDF_ENABLE_ASAN
1714 append_config(
"ASAN");
1716#ifdef PDF_ENABLE_SKIA
1717 append_config(
"SKIA");
1720 append_config(
"GDI");
1722 printf(
"%s\n", config.c_str());
1725constexpr char kUsageString[] =
1726 "Usage: pdfium_test [OPTION] [FILE]...\n"
1727 " --show-config - print build options and exit\n"
1728 " --show-metadata - print the file metadata\n"
1729 " --show-pageinfo - print information about pages\n"
1730 " --show-structure - print the structure elements from the "
1732 " --send-events - send input described by .evt file\n"
1733 " --mem-document - load document with FPDF_LoadMemDocument()\n"
1734 " --render-oneshot - render image without using progressive "
1736 " --lcd-text - render text optimized for LCD displays\n"
1737 " --no-nativetext - render without using the native text output\n"
1738 " --grayscale - render grayscale output\n"
1739 " --forced-color - render in forced color mode\n"
1740 " --fill-to-stroke - render fill as stroke in forced color mode\n"
1741 " --limit-cache - render limiting image cache size\n"
1742 " --force-halftone - render forcing halftone\n"
1743 " --printing - render as if for printing\n"
1744 " --no-smoothtext - render disabling text anti-aliasing\n"
1745 " --no-smoothimage - render disabling image anti-alisasing\n"
1746 " --no-smoothpath - render disabling path anti-aliasing\n"
1747 " --reverse-byte-order - render to BGRA, if supported by the output "
1749 " --save-attachments - write embedded attachments "
1750 "<pdf-name>.attachment.<attachment-name>\n"
1751 " --save-images - write raw embedded images "
1752 "<pdf-name>.<page-number>.<object-number>.png\n"
1753 " --save-rendered-images - write embedded images as rendered on the page "
1754 "<pdf-name>.<page-number>.<object-number>.png\n"
1755 " --save-thumbs - write page thumbnails "
1756 "<pdf-name>.thumbnail.<page-number>.png\n"
1757 " --save-thumbs-dec - write page thumbnails' decoded stream data"
1758 "<pdf-name>.thumbnail.decoded.<page-number>.png\n"
1759 " --save-thumbs-raw - write page thumbnails' raw stream data"
1760 "<pdf-name>.thumbnail.raw.<page-number>.png\n"
1762#if defined(PDF_ENABLE_SKIA)
1764 " --use-renderer - renderer to use, one of [agg | gdi | skia]\n"
1766 " --use-renderer - renderer to use, one of [agg | skia]\n"
1770 " --use-renderer - renderer to use, one of [agg | gdi]\n"
1772 " --use-renderer - renderer to use, one of [agg]\n"
1777 " --disable-javascript - do not execute JS in PDF files\n"
1778 " --js-flags=<flags> - additional flags to pass to V8\n"
1779#ifdef PDF_ENABLE_XFA
1780 " --disable-xfa - do not process XFA forms\n"
1783#ifdef ENABLE_CALLGRIND
1784 " --callgrind-delim - delimit interesting section when using "
1787#if defined(__APPLE__) || (defined(__linux__
) && !defined(__ANDROID__))
1788 " --no-system-fonts - do not use system fonts, overrides --font-dir\n"
1790 " --croscore-font-names - use Croscore font names\n"
1791 " --bin-dir=<path> - override path to v8 external data\n"
1792 " --font-dir=<path> - override path to external fonts\n"
1793 " --scale=<number> - scale output size by number (e.g. 0.5)\n"
1794 " --password=<secret> - password to decrypt the PDF with\n"
1795 " --pages=<number>(-<number>) - only render the given 0-based page(s)\n"
1797 " --bmp - write page images <pdf-name>.<page-number>.bmp\n"
1798 " --emf - write page meta files <pdf-name>.<page-number>.emf\n"
1799 " --ps2 - write page raw PostScript (Lvl 2) "
1800 "<pdf-name>.<page-number>.ps\n"
1801 " --ps3 - write page raw PostScript (Lvl 3) "
1802 "<pdf-name>.<page-number>.ps\n"
1803 " --ps3-type42 - write page raw PostScript (Lvl 3 with Type 42 fonts) "
1804 "<pdf-name>.<page-number>.ps\n"
1806 " --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
1807 " --png - write page images <pdf-name>.<page-number>.png\n"
1808 " --ppm - write page images <pdf-name>.<page-number>.ppm\n"
1809 " --annot - write annotation info <pdf-name>.<page-number>.annot.txt\n"
1810#ifdef PDF_ENABLE_SKIA
1811 " --skp - write page images <pdf-name>.<page-number>.skp\n"
1813 " --xps - write page images <pdf-name>.<page-number>.xps\n"
1816 " --md5 - write output image paths and their md5 hashes to stdout.\n"
1817 " --time=<number> - Seconds since the epoch to set system time.\n"
1820void SetUpErrorHandling() {
1824 SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |
1825 SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
1826 _set_error_mode(_OUT_TO_STDERR);
1827 _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
1828 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
1829 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
1835int main(
int argc,
const char* argv[]) {
1836#if defined(PDF_USE_PARTITION_ALLOC)
1837 pdfium::ConfigurePartitionAllocShimPartitionForTest();
1840 SetUpErrorHandling();
1841 setlocale(LC_CTYPE,
"en_US.UTF-8");
1843 std::vector<std::string> args(argv, argv + argc);
1845 std::vector<std::string> files;
1846 if (!ParseCommandLine(args, &options, &files)) {
1847 fprintf(stderr,
"%s", kUsageString);
1851 if (options.show_config) {
1856 if (files.empty()) {
1857 fprintf(stderr,
"No input files.\n");
1861 FPDF_LIBRARY_CONFIG config;
1863 config.m_pUserFontPaths =
nullptr;
1864 config.m_pIsolate =
nullptr;
1865 config.m_v8EmbedderSlot = 0;
1866 config.m_pPlatform =
nullptr;
1868 switch (options.use_renderer_type) {
1869 case RendererType::kDefault:
1873 case RendererType::kAgg:
1874 config.m_RendererType = FPDF_RENDERERTYPE_AGG;
1878 case RendererType::kGdi:
1880 config.m_RendererType = GetDefaultRendererType();
1884#if defined(PDF_ENABLE_SKIA)
1885 case RendererType::kSkia:
1886#if defined(BUILD_WITH_CHROMIUM)
1889 chromium_support::InitializeDiscardableMemoryAllocator();
1891 config.m_RendererType = FPDF_RENDERERTYPE_SKIA;
1896 std::function<
void()> idler = []() {};
1898#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1899 v8::StartupData snapshot;
1901 std::unique_ptr<v8::Platform> platform;
1902 std::unique_ptr<v8::Isolate, V8IsolateDeleter> isolate;
1903 if (!options.disable_javascript) {
1904#ifdef V8_USE_EXTERNAL_STARTUP_DATA
1905 platform = InitializeV8ForPDFiumWithStartupData(
1906 options.exe_path, options.js_flags, options.bin_directory, &snapshot);
1908 platform = InitializeV8ForPDFium(options.exe_path, options.js_flags);
1911 fprintf(stderr,
"V8 initialization failed.\n");
1914 config.m_pPlatform = platform.get();
1916 v8::Isolate::CreateParams params;
1917 params.array_buffer_allocator =
static_cast<v8::ArrayBuffer::Allocator*>(
1918 FPDF_GetArrayBufferAllocatorSharedInstance());
1919 isolate.reset(v8::Isolate::New(params));
1920 config.m_pIsolate = isolate.get();
1922 idler = [&platform, &isolate]() {
1924 while (v8::platform::PumpMessageLoop(platform.get(), isolate.get()))
1927 fprintf(stderr,
"Pumped %d tasks\n", task_count);
1932 const char* path_array[2] = {
nullptr,
nullptr};
1933 absl::optional<
const char*> custom_font_path = GetCustomFontPath(options);
1934 if (custom_font_path.has_value()) {
1935 path_array[0] = custom_font_path.value();
1936 config.m_pUserFontPaths = path_array;
1941 std::unique_ptr<FontRenamer> font_renamer;
1942 if (options.croscore_font_names)
1943 font_renamer =
std::make_unique<FontRenamer>();
1945 UNSUPPORT_INFO unsupported_info = {};
1951 if (options.time > -1) {
1954 static time_t time_ret = options.time;
1959 Processor processor(&options, &idler);
1960 for (
const std::string& filename : files) {
1961 std::vector<uint8_t> file_contents = GetFileContents(filename.c_str());
1962 if (file_contents.empty()) {
1965 fprintf(stderr,
"Processing PDF file %s.\n", filename.c_str());
1967#ifdef ENABLE_CALLGRIND
1968 if (options.callgrind_delimiters)
1969 CALLGRIND_START_INSTRUMENTATION;
1973 if (options.send_events) {
1974 std::string event_filename = filename;
1975 size_t extension_pos = event_filename.find(
".pdf");
1976 if (extension_pos != std::string::npos) {
1977 event_filename.replace(extension_pos, 4,
".evt");
1978 if (access(event_filename.c_str(), R_OK) == 0) {
1979 fprintf(stderr,
"Using event file %s.\n", event_filename.c_str());
1980 std::vector<uint8_t> event_contents =
1981 GetFileContents(event_filename.c_str());
1982 if (!event_contents.empty()) {
1983 fprintf(stderr,
"Sending events from: %s\n",
1984 event_filename.c_str());
1985 std::copy(event_contents.begin(), event_contents.end(),
1986 std::back_inserter(events));
1992 processor.ProcessPdf(filename, file_contents, events);
1994#ifdef ENABLE_CALLGRIND
1995 if (options.callgrind_delimiters)
1996 CALLGRIND_STOP_INSTRUMENTATION;
2003 if (!options.disable_javascript) {
2005 ShutdownV8ForPDFium();
2006#ifdef V8_USE_EXTERNAL_STARTUP_DATA
2007 free(
const_cast<
char*>(snapshot.data));
static int GetBlock(void *param, unsigned long pos, unsigned char *pBuf, unsigned long size)
FPDF_RENDERER_TYPE GetDefaultRendererType()
void DumpPageStructure(FPDF_PAGE page, int page_idx)
void DumpPageInfo(FPDF_PAGE page, int page_idx)
void SendPageEvents(FPDF_FORMHANDLE form, FPDF_PAGE page, const std::string &events, const std::function< void()> &idler)
#define PDF_FORM_NOTAVAIL
#define PDF_DATA_NOTAVAIL
#define FPDF_UNSP_DOC_PORTABLECOLLECTION
#define FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA
#define FPDF_UNSP_ANNOT_ATTACHMENT
#define FPDF_UNSP_DOC_SECURITY
#define FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM
FPDF_EXPORT void FPDF_CALLCONV FSDK_SetLocaltimeFunction(struct tm *(*func)(const time_t *))
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FSDK_SetUnSpObjProcessHandler(UNSUPPORT_INFO *unsp_info)
#define FPDF_UNSP_DOC_ATTACHMENT
#define FPDF_UNSP_ANNOT_SIG
#define FPDF_UNSP_ANNOT_SOUND
#define FPDF_UNSP_DOC_SHAREDFORM_EMAIL
#define FPDF_UNSP_DOC_XFAFORM
#define FPDF_UNSP_DOC_SHAREDFORM_ACROBAT
#define FPDF_UNSP_ANNOT_3DANNOT
#define FPDF_UNSP_ANNOT_MOVIE
FPDF_EXPORT void FPDF_CALLCONV FSDK_SetTimeFunction(time_t(*func)())
#define FPDF_UNSP_ANNOT_SCREEN_MEDIA
#define FPDF_UNSP_DOC_SHAREDREVIEW
#define FPDF_RENDER_TOBECONTINUED
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetLastError()
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_LoadCustomDocument(FPDF_FILEACCESS *pFileAccess, FPDF_BYTESTRING password)
FPDF_EXPORT float FPDF_CALLCONV FPDF_GetPageHeightF(FPDF_PAGE page)
FPDF_EXPORT void FPDF_CALLCONV FPDF_DestroyLibrary()
FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetStride(FPDF_BITMAP bitmap)
FPDF_EXPORT void FPDF_CALLCONV FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG *config)
FPDF_EXPORT void *FPDF_CALLCONV FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap)
FPDF_EXPORT float FPDF_CALLCONV FPDF_GetPageWidthF(FPDF_PAGE page)
#define FPDF_RENDER_LIMITEDIMAGECACHE
#define FPDF_RENDER_NO_SMOOTHTEXT
#define FPDF_ERR_SECURITY
#define FPDF_RENDER_NO_SMOOTHIMAGE
#define FPDF_CONVERT_FILL_TO_STROKE
#define FPDF_REVERSE_BYTE_ORDER
#define FPDF_ERR_PASSWORD
#define FPDF_RENDER_FORCEHALFTONE
#define FPDF_NO_NATIVETEXT
#define FPDF_RENDER_NO_SMOOTHPATH
int main(int argc, const char *argv[])
void(* AddSegment)(struct _FX_DOWNLOADHINTS *pThis, size_t offset, size_t size)
void(* FSDK_UnSupport_Handler)(struct _UNSUPPORT_INFO *pThis, int nType)
void WriteAnnot(FPDF_PAGE page, const char *pdf_name, int num)
std::string WritePng(const char *pdf_name, int num, void *buffer, int stride, int width, int height)
void WriteText(FPDF_TEXTPAGE textpage, const char *pdf_name, int num)
std::string WritePpm(const char *pdf_name, int num, void *buffer_void, int stride, int width, int height)