1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bitset>
#include <iterator>
#include <map>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#if defined PDF_ENABLE_SKIA && !defined _SKIA_SUPPORT_
#define _SKIA_SUPPORT_
#endif
#include "public/cpp/fpdf_deleters.h"
#include "public/fpdf_annot.h"
#include "public/fpdf_dataavail.h"
#include "public/fpdf_edit.h"
#include "public/fpdf_ext.h"
#include "public/fpdf_formfill.h"
#include "public/fpdf_progressive.h"
#include "public/fpdf_structtree.h"
#include "public/fpdf_text.h"
#include "public/fpdfview.h"
#include "testing/image_diff/image_diff_png.h"
#include "testing/test_support.h"
#include "third_party/base/logging.h"
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#ifdef ENABLE_CALLGRIND
#include <valgrind/callgrind.h>
#endif // ENABLE_CALLGRIND
#ifdef PDF_ENABLE_V8
#include "v8/include/libplatform/libplatform.h"
#include "v8/include/v8.h"
#endif // PDF_ENABLE_V8
#ifdef PDF_ENABLE_SKIA
#include "third_party/skia/include/core/SkPictureRecorder.h"
#include "third_party/skia/include/core/SkStream.h"
#endif
#ifdef _WIN32
#define access _access
#define snprintf _snprintf
#define R_OK 4
#endif
enum OutputFormat {
OUTPUT_NONE,
OUTPUT_STRUCTURE,
OUTPUT_TEXT,
OUTPUT_PPM,
OUTPUT_PNG,
OUTPUT_ANNOT,
#ifdef _WIN32
OUTPUT_BMP,
OUTPUT_EMF,
OUTPUT_PS2,
OUTPUT_PS3,
#endif
#ifdef PDF_ENABLE_SKIA
OUTPUT_SKP,
#endif
};
struct Options {
Options()
: show_config(false),
show_metadata(false),
send_events(false),
render_oneshot(false),
#ifdef ENABLE_CALLGRIND
callgrind_delimiters(false),
#endif // ENABLE_CALLGRIND
pages(false),
md5(false),
output_format(OUTPUT_NONE) {
}
bool show_config;
bool show_metadata;
bool send_events;
bool render_oneshot;
#ifdef ENABLE_CALLGRIND
bool callgrind_delimiters;
#endif // ENABLE_CALLGRIND
bool pages;
bool md5;
OutputFormat output_format;
std::string scale_factor_as_string;
std::string exe_path;
std::string bin_directory;
std::string font_directory;
// 0-based page numbers to be rendered.
int first_page;
int last_page;
};
struct FPDF_FORMFILLINFO_PDFiumTest : public FPDF_FORMFILLINFO {
// Hold a map of the currently loaded pages in order to avoid them
// to get loaded twice.
std::map<int, FPDF_PAGE> loaded_pages;
// Hold a pointer of FPDF_FORMHANDLE so that PDFium app hooks can
// make use of it.
FPDF_FORMHANDLE form_handle;
};
static FPDF_FORMFILLINFO_PDFiumTest* ToPDFiumTestFormFillInfo(
FPDF_FORMFILLINFO* form_fill_info) {
return static_cast<FPDF_FORMFILLINFO_PDFiumTest*>(form_fill_info);
}
static bool CheckDimensions(int stride, int width, int height) {
if (stride < 0 || width < 0 || height < 0)
return false;
if (height > 0 && width > INT_MAX / height)
return false;
return true;
}
static void OutputMD5Hash(const char* file_name, const char* buffer, int len) {
// Get the MD5 hash and write it to stdout.
std::string hash =
GenerateMD5Base16(reinterpret_cast<const uint8_t*>(buffer), len);
printf("MD5:%s:%s\n", file_name, hash.c_str());
}
static std::string WritePpm(const char* pdf_name,
int num,
const void* buffer_void,
int stride,
int width,
int height) {
const char* buffer = reinterpret_cast<const char*>(buffer_void);
if (!CheckDimensions(stride, width, height))
return "";
int out_len = width * height;
if (out_len > INT_MAX / 3)
return "";
out_len *= 3;
char filename[256];
snprintf(filename, sizeof(filename), "%s.%d.ppm", pdf_name, num);
FILE* fp = fopen(filename, "wb");
if (!fp)
return "";
fprintf(fp, "P6\n# PDF test render\n%d %d\n255\n", width, height);
// Source data is B, G, R, unused.
// Dest data is R, G, B.
std::vector<char> result(out_len);
for (int h = 0; h < height; ++h) {
const char* src_line = buffer + (stride * h);
char* dest_line = result.data() + (width * h * 3);
for (int w = 0; w < width; ++w) {
// R
dest_line[w * 3] = src_line[(w * 4) + 2];
// G
dest_line[(w * 3) + 1] = src_line[(w * 4) + 1];
// B
dest_line[(w * 3) + 2] = src_line[w * 4];
}
}
fwrite(result.data(), out_len, 1, fp);
fclose(fp);
return std::string(filename);
}
void WriteText(FPDF_PAGE page, const char* pdf_name, int num) {
char filename[256];
int chars_formatted =
snprintf(filename, sizeof(filename), "%s.%d.txt", pdf_name, num);
if (chars_formatted < 0 ||
static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
fprintf(stderr, "Filename %s is too long\n", filename);
return;
}
FILE* fp = fopen(filename, "w");
if (!fp) {
fprintf(stderr, "Failed to open %s for output\n", filename);
return;
}
// Output in UTF32-LE.
uint32_t bom = 0x0000FEFF;
fwrite(&bom, sizeof(bom), 1, fp);
std::unique_ptr<void, FPDFTextPageDeleter> textpage(FPDFText_LoadPage(page));
for (int i = 0; i < FPDFText_CountChars(textpage.get()); i++) {
uint32_t c = FPDFText_GetUnicode(textpage.get(), i);
fwrite(&c, sizeof(c), 1, fp);
}
(void)fclose(fp);
}
std::string AnnotSubtypeToString(FPDF_ANNOTATION_SUBTYPE subtype) {
if (subtype == FPDF_ANNOT_TEXT)
return "Text";
if (subtype == FPDF_ANNOT_LINK)
return "Link";
if (subtype == FPDF_ANNOT_FREETEXT)
return "FreeText";
if (subtype == FPDF_ANNOT_LINE)
return "Line";
if (subtype == FPDF_ANNOT_SQUARE)
return "Square";
if (subtype == FPDF_ANNOT_CIRCLE)
return "Circle";
if (subtype == FPDF_ANNOT_POLYGON)
return "Polygon";
if (subtype == FPDF_ANNOT_POLYLINE)
return "PolyLine";
if (subtype == FPDF_ANNOT_HIGHLIGHT)
return "Highlight";
if (subtype == FPDF_ANNOT_UNDERLINE)
return "Underline";
if (subtype == FPDF_ANNOT_SQUIGGLY)
return "Squiggly";
if (subtype == FPDF_ANNOT_STRIKEOUT)
return "StrikeOut";
if (subtype == FPDF_ANNOT_STAMP)
return "Stamp";
if (subtype == FPDF_ANNOT_CARET)
return "Caret";
if (subtype == FPDF_ANNOT_INK)
return "Ink";
if (subtype == FPDF_ANNOT_POPUP)
return "Popup";
if (subtype == FPDF_ANNOT_FILEATTACHMENT)
return "FileAttachment";
if (subtype == FPDF_ANNOT_SOUND)
return "Sound";
if (subtype == FPDF_ANNOT_MOVIE)
return "Movie";
if (subtype == FPDF_ANNOT_WIDGET)
return "Widget";
if (subtype == FPDF_ANNOT_SCREEN)
return "Screen";
if (subtype == FPDF_ANNOT_PRINTERMARK)
return "PrinterMark";
if (subtype == FPDF_ANNOT_TRAPNET)
return "TrapNet";
if (subtype == FPDF_ANNOT_WATERMARK)
return "Watermark";
if (subtype == FPDF_ANNOT_THREED)
return "3D";
if (subtype == FPDF_ANNOT_RICHMEDIA)
return "RichMedia";
if (subtype == FPDF_ANNOT_XFAWIDGET)
return "XFAWidget";
NOTREACHED();
return "";
}
std::string AnnotFlagsToString(int flags) {
std::string str = "";
if (flags & FPDF_ANNOT_FLAG_INVISIBLE)
str += "Invisible";
if (flags & FPDF_ANNOT_FLAG_HIDDEN)
str += std::string(str.empty() ? "" : ", ") + "Hidden";
if (flags & FPDF_ANNOT_FLAG_PRINT)
str += std::string(str.empty() ? "" : ", ") + "Print";
if (flags & FPDF_ANNOT_FLAG_NOZOOM)
str += std::string(str.empty() ? "" : ", ") + "NoZoom";
if (flags & FPDF_ANNOT_FLAG_NOROTATE)
str += std::string(str.empty() ? "" : ", ") + "NoRotate";
if (flags & FPDF_ANNOT_FLAG_NOVIEW)
str += std::string(str.empty() ? "" : ", ") + "NoView";
if (flags & FPDF_ANNOT_FLAG_READONLY)
str += std::string(str.empty() ? "" : ", ") + "ReadOnly";
if (flags & FPDF_ANNOT_FLAG_LOCKED)
str += std::string(str.empty() ? "" : ", ") + "Locked";
if (flags & FPDF_ANNOT_FLAG_TOGGLENOVIEW)
str += std::string(str.empty() ? "" : ", ") + "ToggleNoView";
return str;
}
std::string PageObjectTypeToString(int type) {
if (type == FPDF_PAGEOBJ_TEXT)
return "Text";
if (type == FPDF_PAGEOBJ_PATH)
return "Path";
if (type == FPDF_PAGEOBJ_IMAGE)
return "Image";
if (type == FPDF_PAGEOBJ_SHADING)
return "Shading";
if (type == FPDF_PAGEOBJ_FORM)
return "Form";
NOTREACHED();
return "";
}
void WriteAnnot(FPDF_PAGE page, const char* pdf_name, int num) {
// Open the output text file.
char filename[256];
int chars_formatted =
snprintf(filename, sizeof(filename), "%s.%d.annot.txt", pdf_name, num);
if (chars_formatted < 0 ||
static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
fprintf(stderr, "Filename %s is too long\n", filename);
return;
}
FILE* fp = fopen(filename, "w");
if (!fp) {
fprintf(stderr, "Failed to open %s for output\n", filename);
return;
}
int annot_count = FPDFPage_GetAnnotCount(page);
fprintf(fp, "Number of annotations: %d\n\n", annot_count);
// Iterate through all annotations on this page.
for (int i = 0; i < annot_count; ++i) {
// Retrieve the annotation object and its subtype.
fprintf(fp, "Annotation #%d:\n", i + 1);
FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, i);
if (!annot) {
fprintf(fp, "Failed to retrieve annotation!\n\n");
continue;
}
FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot);
fprintf(fp, "Subtype: %s\n", AnnotSubtypeToString(subtype).c_str());
// Retrieve the annotation flags.
fprintf(fp, "Flags set: %s\n",
AnnotFlagsToString(FPDFAnnot_GetFlags(annot)).c_str());
// Retrieve the annotation's object count and object types.
const int obj_count = FPDFAnnot_GetObjectCount(annot);
fprintf(fp, "Number of objects: %d\n", obj_count);
if (obj_count > 0) {
fprintf(fp, "Object types: ");
for (int j = 0; j < obj_count; ++j) {
fprintf(fp, "%s ",
PageObjectTypeToString(
FPDFPageObj_GetType(FPDFAnnot_GetObject(annot, j)))
.c_str());
}
fprintf(fp, "\n");
}
// Retrieve the annotation's color and interior color.
unsigned int R;
unsigned int G;
unsigned int B;
unsigned int A;
if (!FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_Color, &R, &G, &B, &A)) {
fprintf(fp, "Failed to retrieve color.\n");
} else {
fprintf(fp, "Color in RGBA: %d %d %d %d\n", R, G, B, A);
}
if (!FPDFAnnot_GetColor(annot, FPDFANNOT_COLORTYPE_InteriorColor, &R, &G,
&B, &A)) {
fprintf(fp, "Failed to retrieve interior color.\n");
} else {
fprintf(fp, "Interior color in RGBA: %d %d %d %d\n", R, G, B, A);
}
// Retrieve the annotation's contents and author.
std::unique_ptr<unsigned short, pdfium::FreeDeleter> contents_key =
GetFPDFWideString(L"Contents");
unsigned long len =
FPDFAnnot_GetStringValue(annot, contents_key.get(), nullptr, 0);
std::vector<char> buf(len);
FPDFAnnot_GetStringValue(annot, contents_key.get(), buf.data(), len);
fprintf(fp, "Content: %ls\n",
GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
.c_str());
std::unique_ptr<unsigned short, pdfium::FreeDeleter> author_key =
GetFPDFWideString(L"T");
len = FPDFAnnot_GetStringValue(annot, author_key.get(), nullptr, 0);
buf.clear();
buf.resize(len);
FPDFAnnot_GetStringValue(annot, author_key.get(), buf.data(), len);
fprintf(fp, "Author: %ls\n",
GetPlatformWString(reinterpret_cast<unsigned short*>(buf.data()))
.c_str());
// Retrieve the annotation's quadpoints if it is a markup annotation.
if (FPDFAnnot_HasAttachmentPoints(annot)) {
FS_QUADPOINTSF quadpoints = FPDFAnnot_GetAttachmentPoints(annot);
fprintf(fp,
"Quadpoints: (%.3f, %.3f), (%.3f, %.3f), (%.3f, %.3f), (%.3f, "
"%.3f)\n",
quadpoints.x1, quadpoints.y1, quadpoints.x2, quadpoints.y2,
quadpoints.x3, quadpoints.y3, quadpoints.x4, quadpoints.y4);
}
// Retrieve the annotation's rectangle coordinates.
FS_RECTF rect = FPDFAnnot_GetRect(annot);
fprintf(fp, "Rectangle: l - %.3f, b - %.3f, r - %.3f, t - %.3f\n\n",
rect.left, rect.bottom, rect.right, rect.top);
FPDFPage_CloseAnnot(annot);
}
(void)fclose(fp);
}
static std::string WritePng(const char* pdf_name,
int num,
const void* buffer_void,
int stride,
int width,
int height) {
if (!CheckDimensions(stride, width, height))
return "";
std::vector<unsigned char> png_encoding;
const unsigned char* buffer = static_cast<const unsigned char*>(buffer_void);
if (!image_diff_png::EncodeBGRAPNG(
buffer, width, height, stride, false, &png_encoding)) {
fprintf(stderr, "Failed to convert bitmap to PNG\n");
return "";
}
char filename[256];
int chars_formatted = snprintf(
filename, sizeof(filename), "%s.%d.png", pdf_name, num);
if (chars_formatted < 0 ||
static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
fprintf(stderr, "Filename %s is too long\n", filename);
return "";
}
FILE* fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Failed to open %s for output\n", filename);
return "";
}
size_t bytes_written = fwrite(
&png_encoding.front(), 1, png_encoding.size(), fp);
if (bytes_written != png_encoding.size())
fprintf(stderr, "Failed to write to %s\n", filename);
(void)fclose(fp);
return std::string(filename);
}
#ifdef _WIN32
static std::string WriteBmp(const char* pdf_name,
int num,
const void* buffer,
int stride,
int width,
int height) {
if (!CheckDimensions(stride, width, height))
return "";
int out_len = stride * height;
if (out_len > INT_MAX / 3)
return "";
char filename[256];
snprintf(filename, sizeof(filename), "%s.%d.bmp", pdf_name, num);
FILE* fp = fopen(filename, "wb");
if (!fp)
return "";
BITMAPINFO bmi = {};
bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -height; // top-down image
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
BITMAPFILEHEADER file_header = {};
file_header.bfType = 0x4d42;
file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
file_header.bfOffBits = file_header.bfSize - out_len;
fwrite(&file_header, sizeof(file_header), 1, fp);
fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
fwrite(buffer, out_len, 1, fp);
fclose(fp);
return std::string(filename);
}
void WriteEmf(FPDF_PAGE page, const char* pdf_name, int num) {
char filename[256];
snprintf(filename, sizeof(filename), "%s.%d.emf", pdf_name, num);
HDC dc = CreateEnhMetaFileA(nullptr, filename, nullptr, nullptr);
int width = static_cast<int>(FPDF_GetPageWidth(page));
int height = static_cast<int>(FPDF_GetPageHeight(page));
HRGN rgn = CreateRectRgn(0, 0, width, height);
SelectClipRgn(dc, rgn);
DeleteObject(rgn);
SelectObject(dc, GetStockObject(NULL_PEN));
SelectObject(dc, GetStockObject(WHITE_BRUSH));
// If a PS_NULL pen is used, the dimensions of the rectangle are 1 pixel less.
Rectangle(dc, 0, 0, width + 1, height + 1);
FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
DeleteEnhMetaFile(CloseEnhMetaFile(dc));
}
int CALLBACK EnhMetaFileProc(HDC hdc,
HANDLETABLE* handle_table,
const ENHMETARECORD* record,
int objects_count,
LPARAM param) {
std::vector<const ENHMETARECORD*>& items =
*reinterpret_cast<std::vector<const ENHMETARECORD*>*>(param);
items.push_back(record);
return 1;
}
void WritePS(FPDF_PAGE page, const char* pdf_name, int num) {
char filename[256];
snprintf(filename, sizeof(filename), "%s.%d.ps", pdf_name, num);
FILE* fp = fopen(filename, "wb");
if (!fp)
return;
HDC dc = CreateEnhMetaFileA(nullptr, nullptr, nullptr, nullptr);
int width = static_cast<int>(FPDF_GetPageWidth(page));
int height = static_cast<int>(FPDF_GetPageHeight(page));
FPDF_RenderPage(dc, page, 0, 0, width, height, 0,
FPDF_ANNOT | FPDF_PRINTING | FPDF_NO_CATCH);
HENHMETAFILE emf = CloseEnhMetaFile(dc);
std::vector<const ENHMETARECORD*> items;
EnumEnhMetaFile(nullptr, emf, &EnhMetaFileProc, &items, nullptr);
for (const ENHMETARECORD* record : items) {
if (record->iType != EMR_GDICOMMENT)
continue;
const auto* comment = reinterpret_cast<const EMRGDICOMMENT*>(record);
const char* data = reinterpret_cast<const char*>(comment->Data);
uint16_t size = *reinterpret_cast<const uint16_t*>(data);
fwrite(data + sizeof(uint16_t), size, 1, fp);
}
fclose(fp);
DeleteEnhMetaFile(emf);
}
#endif // _WIN32
#ifdef PDF_ENABLE_SKIA
static std::string WriteSkp(const char* pdf_name,
int num,
SkPictureRecorder* recorder) {
char filename[256];
int chars_formatted =
snprintf(filename, sizeof(filename), "%s.%d.skp", pdf_name, num);
if (chars_formatted < 0 ||
static_cast<size_t>(chars_formatted) >= sizeof(filename)) {
fprintf(stderr, "Filename %s is too long\n", filename);
return "";
}
sk_sp<SkPicture> picture(recorder->finishRecordingAsPicture());
SkFILEWStream wStream(filename);
picture->serialize(&wStream);
return std::string(filename);
}
#endif
// These example JS platform callback handlers are entirely optional,
// and exist here to show the flow of information from a document back
// to the embedder.
int ExampleAppAlert(IPDF_JSPLATFORM*,
FPDF_WIDESTRING msg,
FPDF_WIDESTRING title,
int type,
int icon) {
printf("%ls", GetPlatformWString(title).c_str());
if (icon || type)
printf("[icon=%d,type=%d]", icon, type);
printf(": %ls\n", GetPlatformWString(msg).c_str());
return 0;
}
int ExampleAppResponse(IPDF_JSPLATFORM*,
FPDF_WIDESTRING question,
FPDF_WIDESTRING title,
FPDF_WIDESTRING default_value,
FPDF_WIDESTRING label,
FPDF_BOOL is_password,
void* response,
int length) {
printf("%ls: %ls, defaultValue=%ls, label=%ls, isPassword=%d, length=%d\n",
GetPlatformWString(title).c_str(),
GetPlatformWString(question).c_str(),
GetPlatformWString(default_value).c_str(),
GetPlatformWString(label).c_str(), is_password, length);
// UTF-16, always LE regardless of platform.
uint8_t* ptr = static_cast<uint8_t*>(response);
ptr[0] = 'N';
ptr[1] = 0;
ptr[2] = 'o';
ptr[3] = 0;
return 4;
}
void ExampleDocGotoPage(IPDF_JSPLATFORM*, int page_number) {
printf("Goto Page: %d\n", page_number);
}
void ExampleDocMail(IPDF_JSPLATFORM*,
void* mailData,
int length,
FPDF_BOOL UI,
FPDF_WIDESTRING To,
FPDF_WIDESTRING Subject,
FPDF_WIDESTRING CC,
FPDF_WIDESTRING BCC,
FPDF_WIDESTRING Msg) {
printf("Mail Msg: %d, to=%ls, cc=%ls, bcc=%ls, subject=%ls, body=%ls\n", UI,
GetPlatformWString(To).c_str(), GetPlatformWString(CC).c_str(),
GetPlatformWString(BCC).c_str(), GetPlatformWString(Subject).c_str(),
GetPlatformWString(Msg).c_str());
}
void ExampleUnsupportedHandler(UNSUPPORT_INFO*, int type) {
std::string feature = "Unknown";
switch (type) {
case FPDF_UNSP_DOC_XFAFORM:
feature = "XFA";
break;
case FPDF_UNSP_DOC_PORTABLECOLLECTION:
feature = "Portfolios_Packages";
break;
case FPDF_UNSP_DOC_ATTACHMENT:
case FPDF_UNSP_ANNOT_ATTACHMENT:
feature = "Attachment";
break;
case FPDF_UNSP_DOC_SECURITY:
feature = "Rights_Management";
break;
case FPDF_UNSP_DOC_SHAREDREVIEW:
feature = "Shared_Review";
break;
case FPDF_UNSP_DOC_SHAREDFORM_ACROBAT:
case FPDF_UNSP_DOC_SHAREDFORM_FILESYSTEM:
case FPDF_UNSP_DOC_SHAREDFORM_EMAIL:
feature = "Shared_Form";
break;
case FPDF_UNSP_ANNOT_3DANNOT:
feature = "3D";
break;
case FPDF_UNSP_ANNOT_MOVIE:
feature = "Movie";
break;
case FPDF_UNSP_ANNOT_SOUND:
feature = "Sound";
break;
case FPDF_UNSP_ANNOT_SCREEN_MEDIA:
case FPDF_UNSP_ANNOT_SCREEN_RICHMEDIA:
feature = "Screen";
break;
case FPDF_UNSP_ANNOT_SIG:
feature = "Digital_Signature";
break;
}
printf("Unsupported feature: %s.\n", feature.c_str());
}
bool ParseCommandLine(const std::vector<std::string>& args,
Options* options,
std::vector<std::string>* files) {
if (args.empty())
return false;
options->exe_path = args[0];
size_t cur_idx = 1;
for (; cur_idx < args.size(); ++cur_idx) {
const std::string& cur_arg = args[cur_idx];
if (cur_arg == "--show-config") {
options->show_config = true;
} else if (cur_arg == "--show-metadata") {
options->show_metadata = true;
} else if (cur_arg == "--send-events") {
options->send_events = true;
} else if (cur_arg == "--render-oneshot") {
options->render_oneshot = true;
#ifdef ENABLE_CALLGRIND
} else if (cur_arg == "--callgrind-delim") {
options->callgrind_delimiters = true;
#endif // ENABLE_CALLGRIND
} else if (cur_arg == "--ppm") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --ppm argument\n");
return false;
}
options->output_format = OUTPUT_PPM;
} else if (cur_arg == "--png") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --png argument\n");
return false;
}
options->output_format = OUTPUT_PNG;
} else if (cur_arg == "--txt") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --txt argument\n");
return false;
}
options->output_format = OUTPUT_TEXT;
} else if (cur_arg == "--annot") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --annot argument\n");
return false;
}
options->output_format = OUTPUT_ANNOT;
#ifdef PDF_ENABLE_SKIA
} else if (cur_arg == "--skp") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --skp argument\n");
return false;
}
options->output_format = OUTPUT_SKP;
#endif
} else if (cur_arg.size() > 11 &&
cur_arg.compare(0, 11, "--font-dir=") == 0) {
if (!options->font_directory.empty()) {
fprintf(stderr, "Duplicate --font-dir argument\n");
return false;
}
options->font_directory = cur_arg.substr(11);
#ifdef _WIN32
} else if (cur_arg == "--emf") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --emf argument\n");
return false;
}
options->output_format = OUTPUT_EMF;
} else if (cur_arg == "--ps2") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --ps2 argument\n");
return false;
}
options->output_format = OUTPUT_PS2;
} else if (cur_arg == "--ps3") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --ps3 argument\n");
return false;
}
options->output_format = OUTPUT_PS3;
} else if (cur_arg == "--bmp") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --bmp argument\n");
return false;
}
options->output_format = OUTPUT_BMP;
#endif // _WIN32
#ifdef PDF_ENABLE_V8
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
} else if (cur_arg.size() > 10 &&
cur_arg.compare(0, 10, "--bin-dir=") == 0) {
if (!options->bin_directory.empty()) {
fprintf(stderr, "Duplicate --bin-dir argument\n");
return false;
}
options->bin_directory = cur_arg.substr(10);
#endif // V8_USE_EXTERNAL_STARTUP_DATA
#endif // PDF_ENABLE_V8
} else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--scale=") == 0) {
if (!options->scale_factor_as_string.empty()) {
fprintf(stderr, "Duplicate --scale argument\n");
return false;
}
options->scale_factor_as_string = cur_arg.substr(8);
} else if (cur_arg == "--show-structure") {
if (options->output_format != OUTPUT_NONE) {
fprintf(stderr, "Duplicate or conflicting --show-structure argument\n");
return false;
}
options->output_format = OUTPUT_STRUCTURE;
} else if (cur_arg.size() > 8 && cur_arg.compare(0, 8, "--pages=") == 0) {
if (options->pages) {
fprintf(stderr, "Duplicate --pages argument\n");
return false;
}
options->pages = true;
const std::string pages_string = cur_arg.substr(8);
size_t first_dash = pages_string.find("-");
if (first_dash == std::string::npos) {
std::stringstream(pages_string) >> options->first_page;
options->last_page = options->first_page;
} else {
std::stringstream(pages_string.substr(0, first_dash)) >>
options->first_page;
std::stringstream(pages_string.substr(first_dash + 1)) >>
options->last_page;
}
} else if (cur_arg == "--md5") {
options->md5 = true;
} else if (cur_arg.size() >= 2 && cur_arg[0] == '-' && cur_arg[1] == '-') {
fprintf(stderr, "Unrecognized argument %s\n", cur_arg.c_str());
return false;
} else {
break;
}
}
for (size_t i = cur_idx; i < args.size(); i++)
files->push_back(args[i]);
return true;
}
FPDF_BOOL Is_Data_Avail(FX_FILEAVAIL* avail, size_t offset, size_t size) {
return true;
}
void Add_Segment(FX_DOWNLOADHINTS* hints, size_t offset, size_t size) {}
void SendPageEvents(FPDF_FORMHANDLE form,
FPDF_PAGE page,
const std::string& events) {
auto lines = StringSplit(events, '\n');
for (auto line : lines) {
auto command = StringSplit(line, '#');
if (command[0].empty())
continue;
auto tokens = StringSplit(command[0], ',');
if (tokens[0] == "charcode") {
if (tokens.size() == 2) {
int keycode = atoi(tokens[1].c_str());
FORM_OnChar(form, page, keycode, 0);
} else {
fprintf(stderr, "charcode: bad args\n");
}
} else if (tokens[0] == "keycode") {
if (tokens.size() == 2) {
int keycode = atoi(tokens[1].c_str());
FORM_OnKeyDown(form, page, keycode, 0);
FORM_OnKeyUp(form, page, keycode, 0);
} else {
fprintf(stderr, "keycode: bad args\n");
}
} else if (tokens[0] == "mousedown") {
if (tokens.size() == 4) {
int x = atoi(tokens[2].c_str());
int y = atoi(tokens[3].c_str());
if (tokens[1] == "left")
FORM_OnLButtonDown(form, page, 0, x, y);
#ifdef PDF_ENABLE_XFA
else if (tokens[1] == "right")
FORM_OnRButtonDown(form, page, 0, x, y);
#endif
else
fprintf(stderr, "mousedown: bad button name\n");
} else {
fprintf(stderr, "mousedown: bad args\n");
}
} else if (tokens[0] == "mouseup") {
if (tokens.size() == 4) {
int x = atoi(tokens[2].c_str());
int y = atoi(tokens[3].c_str());
if (tokens[1] == "left")
FORM_OnLButtonUp(form, page, 0, x, y);
#ifdef PDF_ENABLE_XFA
else if (tokens[1] == "right")
FORM_OnRButtonUp(form, page, 0, x, y);
#endif
else
fprintf(stderr, "mouseup: bad button name\n");
} else {
fprintf(stderr, "mouseup: bad args\n");
}
} else if (tokens[0] == "mousemove") {
if (tokens.size() == 3) {
int x = atoi(tokens[1].c_str());
int y = atoi(tokens[2].c_str());
FORM_OnMouseMove(form, page, 0, x, y);
} else {
fprintf(stderr, "mousemove: bad args\n");
}
} else {
fprintf(stderr, "Unrecognized event: %s\n", tokens[0].c_str());
}
}
}
FPDF_PAGE GetPageForIndex(FPDF_FORMFILLINFO* param,
FPDF_DOCUMENT doc,
int index) {
FPDF_FORMFILLINFO_PDFiumTest* form_fill_info =
ToPDFiumTestFormFillInfo(param);
auto& loaded_pages = form_fill_info->loaded_pages;
auto iter = loaded_pages.find(index);
if (iter != loaded_pages.end())
return iter->second;
FPDF_PAGE page = FPDF_LoadPage(doc, index);
if (!page)
return nullptr;
FPDF_FORMHANDLE& form_handle = form_fill_info->form_handle;
FORM_OnAfterLoadPage(page, form_handle);
FORM_DoPageAAction(page, form_handle, FPDFPAGE_AACTION_OPEN);
loaded_pages[index] = page;
return page;
}
std::wstring ConvertToWString(const unsigned short* buf,
unsigned long buf_size) {
std::wstring result;
result.reserve(buf_size);
std::copy(buf, buf + buf_size, std::back_inserter(result));
return result;
}
void DumpChildStructure(FPDF_STRUCTELEMENT child, int indent) {
static const size_t kBufSize = 1024;
unsigned short buf[kBufSize];
unsigned long len = FPDF_StructElement_GetType(child, buf, kBufSize);
printf("%*s%ls", indent * 2, "", ConvertToWString(buf, len).c_str());
memset(buf, 0, sizeof(buf));
len = FPDF_StructElement_GetTitle(child, buf, kBufSize);
if (len > 0)
printf(": '%ls'", ConvertToWString(buf, len).c_str());
memset(buf, 0, sizeof(buf));
len = FPDF_StructElement_GetAltText(child, buf, kBufSize);
if (len > 0)
printf(" (%ls)", ConvertToWString(buf, len).c_str());
printf("\n");
for (int i = 0; i < FPDF_StructElement_CountChildren(child); ++i) {
FPDF_STRUCTELEMENT sub_child = FPDF_StructElement_GetChildAtIndex(child, i);
// If the child is not an Element then this will return null. This can
// happen if the element is things like an object reference or a stream.
if (!sub_child)
continue;
DumpChildStructure(sub_child, indent + 1);
}
}
void DumpPageStructure(FPDF_PAGE page, const int page_idx) {
std::unique_ptr<void, FPDFStructTreeDeleter> tree(
FPDF_StructTree_GetForPage(page));
if (!tree) {
fprintf(stderr, "Failed to load struct tree for page %d\n", page_idx);
return;
}
printf("Structure Tree for Page %d\n", page_idx);
for (int i = 0; i < FPDF_StructTree_CountChildren(tree.get()); ++i) {
FPDF_STRUCTELEMENT child = FPDF_StructTree_GetChildAtIndex(tree.get(), i);
if (!child) {
fprintf(stderr, "Failed to load child %d for page %d\n", i, page_idx);
continue;
}
DumpChildStructure(child, 0);
}
printf("\n\n");
}
// Note, for a client using progressive rendering you'd want to determine if you
// need the rendering to pause instead of always saying |true|. This is for
// testing to force the renderer to break whenever possible.
FPDF_BOOL NeedToPauseNow(IFSDK_PAUSE* p) {
return true;
}
bool RenderPage(const std::string& name,
FPDF_DOCUMENT doc,
FPDF_FORMHANDLE form,
FPDF_FORMFILLINFO_PDFiumTest& form_fill_info,
const int page_index,
const Options& options,
const std::string& events) {
std::unique_ptr<void, FPDFPageDeleter> page(
GetPageForIndex(&form_fill_info, doc, page_index));
if (!page.get())
return false;
if (options.send_events)
SendPageEvents(form, page.get(), events);
if (options.output_format == OUTPUT_STRUCTURE) {
DumpPageStructure(page.get(), page_index);
return true;
}
std::unique_ptr<void, FPDFTextPageDeleter> text_page(
FPDFText_LoadPage(page.get()));
double scale = 1.0;
if (!options.scale_factor_as_string.empty())
std::stringstream(options.scale_factor_as_string) >> scale;
int width = static_cast<int>(FPDF_GetPageWidth(page.get()) * scale);
int height = static_cast<int>(FPDF_GetPageHeight(page.get()) * scale);
int alpha = FPDFPage_HasTransparency(page.get()) ? 1 : 0;
std::unique_ptr<void, FPDFBitmapDeleter> bitmap(
FPDFBitmap_Create(width, height, alpha));
if (bitmap) {
FPDF_DWORD fill_color = alpha ? 0x00000000 : 0xFFFFFFFF;
FPDFBitmap_FillRect(bitmap.get(), 0, 0, width, height, fill_color);
if (options.render_oneshot) {
// Note, client programs probably want to use this method instead of the
// progressive calls. The progressive calls are if you need to pause the
// rendering to update the UI, the PDF renderer will break when possible.
FPDF_RenderPageBitmap(bitmap.get(), page.get(), 0, 0, width, height, 0,
FPDF_ANNOT);
} else {
IFSDK_PAUSE pause;
pause.version = 1;
pause.NeedToPauseNow = &NeedToPauseNow;
int rv = FPDF_RenderPageBitmap_Start(
bitmap.get(), page.get(), 0, 0, width, height, 0, FPDF_ANNOT, &pause);
while (rv == FPDF_RENDER_TOBECOUNTINUED)
rv = FPDF_RenderPage_Continue(page.get(), &pause);
}
FPDF_FFLDraw(form, bitmap.get(), page.get(), 0, 0, width, height, 0,
FPDF_ANNOT);
if (!options.render_oneshot)
FPDF_RenderPage_Close(page.get());
int stride = FPDFBitmap_GetStride(bitmap.get());
const char* buffer =
reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bitmap.get()));
std::string&& image_file_name = "";
switch (options.output_format) {
#ifdef _WIN32
case OUTPUT_BMP:
image_file_name =
WriteBmp(name.c_str(), page_index, buffer, stride, width, height);
break;
case OUTPUT_EMF:
WriteEmf(page.get(), name.c_str(), page_index);
break;
case OUTPUT_PS2:
case OUTPUT_PS3:
WritePS(page.get(), name.c_str(), page_index);
break;
#endif
case OUTPUT_TEXT:
WriteText(page.get(), name.c_str(), page_index);
break;
case OUTPUT_ANNOT:
WriteAnnot(page.get(), name.c_str(), page_index);
break;
case OUTPUT_PNG:
image_file_name =
WritePng(name.c_str(), page_index, buffer, stride, width, height);
break;
case OUTPUT_PPM:
image_file_name =
WritePpm(name.c_str(), page_index, buffer, stride, width, height);
break;
#ifdef PDF_ENABLE_SKIA
case OUTPUT_SKP: {
std::unique_ptr<SkPictureRecorder> recorder(
reinterpret_cast<SkPictureRecorder*>(
FPDF_RenderPageSkp(page.get(), width, height)));
FPDF_FFLRecord(form, recorder.get(), page.get(), 0, 0, width, height, 0,
0);
image_file_name = WriteSkp(name.c_str(), page_index, recorder.get());
} break;
#endif
default:
break;
}
// Write the filename and the MD5 of the buffer to stdout if we wrote a
// file.
if (options.md5 && image_file_name != "")
OutputMD5Hash(image_file_name.c_str(), buffer, stride * height);
} else {
fprintf(stderr, "Page was too large to be rendered.\n");
}
form_fill_info.loaded_pages.erase(page_index);
FORM_DoPageAAction(page.get(), form, FPDFPAGE_AACTION_CLOSE);
FORM_OnBeforeClosePage(page.get(), form);
return !!bitmap;
}
void RenderPdf(const std::string& name,
const char* pBuf,
size_t len,
const Options& options,
const std::string& events) {
IPDF_JSPLATFORM platform_callbacks;
memset(&platform_callbacks, '\0', sizeof(platform_callbacks));
platform_callbacks.version = 3;
platform_callbacks.app_alert = ExampleAppAlert;
platform_callbacks.app_response = ExampleAppResponse;
platform_callbacks.Doc_gotoPage = ExampleDocGotoPage;
platform_callbacks.Doc_mail = ExampleDocMail;
FPDF_FORMFILLINFO_PDFiumTest form_callbacks = {};
#ifdef PDF_ENABLE_XFA
form_callbacks.version = 2;
#else // PDF_ENABLE_XFA
form_callbacks.version = 1;
#endif // PDF_ENABLE_XFA
form_callbacks.FFI_GetPage = GetPageForIndex;
form_callbacks.m_pJsPlatform = &platform_callbacks;
TestLoader loader(pBuf, len);
FPDF_FILEACCESS file_access;
memset(&file_access, '\0', sizeof(file_access));
file_access.m_FileLen = static_cast<unsigned long>(len);
file_access.m_GetBlock = TestLoader::GetBlock;
file_access.m_Param = &loader;
FX_FILEAVAIL file_avail;
memset(&file_avail, '\0', sizeof(file_avail));
file_avail.version = 1;
file_avail.IsDataAvail = Is_Data_Avail;
FX_DOWNLOADHINTS hints;
memset(&hints, '\0', sizeof(hints));
hints.version = 1;
hints.AddSegment = Add_Segment;
int nRet = PDF_DATA_NOTAVAIL;
bool bIsLinearized = false;
std::unique_ptr<void, FPDFDocumentDeleter> doc;
std::unique_ptr<void, FPDFAvailDeleter> pdf_avail(
FPDFAvail_Create(&file_avail, &file_access));
if (FPDFAvail_IsLinearized(pdf_avail.get()) == PDF_LINEARIZED) {
doc.reset(FPDFAvail_GetDocument(pdf_avail.get(), nullptr));
if (doc) {
while (nRet == PDF_DATA_NOTAVAIL)
nRet = FPDFAvail_IsDocAvail(pdf_avail.get(), &hints);
if (nRet == PDF_DATA_ERROR) {
fprintf(stderr, "Unknown error in checking if doc was available.\n");
return;
}
nRet = FPDFAvail_IsFormAvail(pdf_avail.get(), &hints);
if (nRet == PDF_FORM_ERROR || nRet == PDF_FORM_NOTAVAIL) {
fprintf(stderr,
"Error %d was returned in checking if form was available.\n",
nRet);
return;
}
bIsLinearized = true;
}
} else {
doc.reset(FPDF_LoadCustomDocument(&file_access, nullptr));
}
if (!doc) {
unsigned long err = FPDF_GetLastError();
fprintf(stderr, "Load pdf docs unsuccessful: ");
switch (err) {
case FPDF_ERR_SUCCESS:
fprintf(stderr, "Success");
break;
case FPDF_ERR_UNKNOWN:
fprintf(stderr, "Unknown error");
break;
case FPDF_ERR_FILE:
fprintf(stderr, "File not found or could not be opened");
break;
case FPDF_ERR_FORMAT:
fprintf(stderr, "File not in PDF format or corrupted");
break;
case FPDF_ERR_PASSWORD:
fprintf(stderr, "Password required or incorrect password");
break;
case FPDF_ERR_SECURITY:
fprintf(stderr, "Unsupported security scheme");
break;
case FPDF_ERR_PAGE:
fprintf(stderr, "Page not found or content error");
break;
default:
fprintf(stderr, "Unknown error %ld", err);
}
fprintf(stderr, ".\n");
return;
}
(void)FPDF_GetDocPermissions(doc.get());
if (options.show_metadata) {
const char* metaTags[] = {"Title", "Author", "Subject", "Keywords",
"Creator", "Producer", "CreationDate", "ModDate"};
for (const char* metaTag : metaTags) {
char metaBuffer[4096];
int len = FPDF_GetMetaText(doc.get(), metaTag, metaBuffer, 4096);
printf("%-12s = %ls (%d bytes)\n", metaTag,
GetPlatformWString(reinterpret_cast<unsigned short*>(metaBuffer))
.c_str(),
len);
}
}
std::unique_ptr<void, FPDFFormHandleDeleter> form(
FPDFDOC_InitFormFillEnvironment(doc.get(), &form_callbacks));
form_callbacks.form_handle = form.get();
#ifdef PDF_ENABLE_XFA
int doc_type = DOCTYPE_PDF;
if (FPDF_HasXFAField(doc.get(), &doc_type) && doc_type != DOCTYPE_PDF &&
!FPDF_LoadXFA(doc.get())) {
fprintf(stderr, "LoadXFA unsuccessful, continuing anyway.\n");
}
#endif // PDF_ENABLE_XFA
FPDF_SetFormFieldHighlightColor(form.get(), 0, 0xFFE4DD);
FPDF_SetFormFieldHighlightAlpha(form.get(), 100);
FORM_DoDocumentJSAction(form.get());
FORM_DoDocumentOpenAction(form.get());
#if _WIN32
if (options.output_format == OUTPUT_PS2)
FPDF_SetPrintMode(2);
else if (options.output_format == OUTPUT_PS3)
FPDF_SetPrintMode(3);
#endif
int page_count = FPDF_GetPageCount(doc.get());
int rendered_pages = 0;
int bad_pages = 0;
int first_page = options.pages ? options.first_page : 0;
int last_page = options.pages ? options.last_page + 1 : page_count;
for (int i = first_page; i < last_page; ++i) {
if (bIsLinearized) {
nRet = PDF_DATA_NOTAVAIL;
while (nRet == PDF_DATA_NOTAVAIL)
nRet = FPDFAvail_IsPageAvail(pdf_avail.get(), i, &hints);
if (nRet == PDF_DATA_ERROR) {
fprintf(stderr, "Unknown error in checking if page %d is available.\n",
i);
return;
}
}
if (RenderPage(name, doc.get(), form.get(), form_callbacks, i, options,
events))
++rendered_pages;
else
++bad_pages;
}
FORM_DoDocumentAAction(form.get(), FPDFDOC_AACTION_WC);
fprintf(stderr, "Rendered %d pages.\n", rendered_pages);
if (bad_pages)
fprintf(stderr, "Skipped %d bad pages.\n", bad_pages);
}
static void ShowConfig() {
std::string config;
std::string maybe_comma;
#if PDF_ENABLE_V8
config.append(maybe_comma);
config.append("V8");
maybe_comma = ",";
#endif // PDF_ENABLE_V8
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
config.append(maybe_comma);
config.append("V8_EXTERNAL");
maybe_comma = ",";
#endif // V8_USE_EXTERNAL_STARTUP_DATA
#ifdef PDF_ENABLE_XFA
config.append(maybe_comma);
config.append("XFA");
maybe_comma = ",";
#endif // PDF_ENABLE_XFA
#ifdef PDF_ENABLE_ASAN
config.append(maybe_comma);
config.append("ASAN");
maybe_comma = ",";
#endif // PDF_ENABLE_ASAN
printf("%s\n", config.c_str());
}
static const char kUsageString[] =
"Usage: pdfium_test [OPTION] [FILE]...\n"
" --show-config - print build options and exit\n"
" --show-metadata - print the file metadata\n"
" --show-structure - print the structure elements from the document\n"
" --send-events - send input described by .evt file\n"
" --render-oneshot - render image without using progressive renderer\n"
#ifdef ENABLE_CALLGRIND
" --callgrind-delim - delimit interesting section when using callgrind\n"
#endif // ENABLE_CALLGRIND
" --bin-dir=<path> - override path to v8 external data\n"
" --font-dir=<path> - override path to external fonts\n"
" --scale=<number> - scale output size by number (e.g. 0.5)\n"
" --pages=<number>(-<number>) - only render the given 0-based page(s)\n"
#ifdef _WIN32
" --bmp - write page images <pdf-name>.<page-number>.bmp\n"
" --emf - write page meta files <pdf-name>.<page-number>.emf\n"
" --ps2 - write page raw PostScript (Lvl 2) "
"<pdf-name>.<page-number>.ps\n"
" --ps3 - write page raw PostScript (Lvl 3) "
"<pdf-name>.<page-number>.ps\n"
#endif // _WIN32
" --txt - write page text in UTF32-LE <pdf-name>.<page-number>.txt\n"
" --png - write page images <pdf-name>.<page-number>.png\n"
" --ppm - write page images <pdf-name>.<page-number>.ppm\n"
" --annot - write annotation info <pdf-name>.<page-number>.annot.txt\n"
#ifdef PDF_ENABLE_SKIA
" --skp - write page images <pdf-name>.<page-number>.skp\n"
#endif
" --md5 - write output image paths and their md5 hashes to stdout.\n"
"";
int main(int argc, const char* argv[]) {
std::vector<std::string> args(argv, argv + argc);
Options options;
std::vector<std::string> files;
if (!ParseCommandLine(args, &options, &files)) {
fprintf(stderr, "%s", kUsageString);
return 1;
}
if (options.show_config) {
ShowConfig();
return 0;
}
if (files.empty()) {
fprintf(stderr, "No input files.\n");
return 1;
}
#ifdef PDF_ENABLE_V8
v8::Platform* platform;
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
v8::StartupData natives;
v8::StartupData snapshot;
InitializeV8ForPDFium(options.exe_path, options.bin_directory, &natives,
&snapshot, &platform);
#else // V8_USE_EXTERNAL_STARTUP_DATA
InitializeV8ForPDFium(options.exe_path, &platform);
#endif // V8_USE_EXTERNAL_STARTUP_DATA
#endif // PDF_ENABLE_V8
FPDF_LIBRARY_CONFIG config;
config.version = 2;
config.m_pUserFontPaths = nullptr;
config.m_pIsolate = nullptr;
config.m_v8EmbedderSlot = 0;
const char* path_array[2];
if (!options.font_directory.empty()) {
path_array[0] = options.font_directory.c_str();
path_array[1] = nullptr;
config.m_pUserFontPaths = path_array;
}
FPDF_InitLibraryWithConfig(&config);
UNSUPPORT_INFO unsupported_info;
memset(&unsupported_info, '\0', sizeof(unsupported_info));
unsupported_info.version = 1;
unsupported_info.FSDK_UnSupport_Handler = ExampleUnsupportedHandler;
FSDK_SetUnSpObjProcessHandler(&unsupported_info);
for (const std::string& filename : files) {
size_t file_length = 0;
std::unique_ptr<char, pdfium::FreeDeleter> file_contents =
GetFileContents(filename.c_str(), &file_length);
if (!file_contents)
continue;
fprintf(stderr, "Rendering PDF file %s.\n", filename.c_str());
#ifdef ENABLE_CALLGRIND
if (options.callgrind_delimiters)
CALLGRIND_START_INSTRUMENTATION;
#endif // ENABLE_CALLGRIND
std::string events;
if (options.send_events) {
std::string event_filename = filename;
size_t event_length = 0;
size_t extension_pos = event_filename.find(".pdf");
if (extension_pos != std::string::npos) {
event_filename.replace(extension_pos, 4, ".evt");
if (access(event_filename.c_str(), R_OK) == 0) {
fprintf(stderr, "Using event file %s.\n", event_filename.c_str());
std::unique_ptr<char, pdfium::FreeDeleter> event_contents =
GetFileContents(event_filename.c_str(), &event_length);
if (event_contents) {
fprintf(stderr, "Sending events from: %s\n",
event_filename.c_str());
events = std::string(event_contents.get(), event_length);
}
}
}
}
RenderPdf(filename, file_contents.get(), file_length, options, events);
#ifdef ENABLE_CALLGRIND
if (options.callgrind_delimiters)
CALLGRIND_STOP_INSTRUMENTATION;
#endif // ENABLE_CALLGRIND
}
FPDF_DestroyLibrary();
#ifdef PDF_ENABLE_V8
v8::V8::ShutdownPlatform();
delete platform;
#ifdef V8_USE_EXTERNAL_STARTUP_DATA
free(const_cast<char*>(natives.data));
free(const_cast<char*>(snapshot.data));
#endif // V8_USE_EXTERNAL_STARTUP_DATA
#endif // PDF_ENABLE_V8
return 0;
}
|