-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf-api.php
executable file
·5384 lines (4333 loc) · 180 KB
/
pdf-api.php
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
<?
################################################################################
# copyright 2008 - 2019 by Markus Olderdissen
# free for private use or inspiration.
# public use need written permission.
################################################################################
define("FONTDESCRIPTOR_FLAG_FIXEDPITCH", 1 << 1);
define("FONTDESCRIPTOR_FLAG_SERIF", 1 << 2);
define("FONTDESCRIPTOR_FLAG_SYMBOLIC", 1 << 3);
define("FONTDESCRIPTOR_FLAG_SCRIPT", 1 << 4);
define("FONTDESCRIPTOR_FLAG_NONSYMBOLIC", 1 << 6);
define("FONTDESCRIPTOR_FLAG_ITALIC", 1 << 7);
define("FONTDESCRIPTOR_FLAG_ALLCAP", 1 << 17);
define("FONTDESCRIPTOR_FLAG_SMALLCAP", 1 << 18);
define("FONTDESCRIPTOR_FLAG_FORCEBOLD", 1 << 19);
################################################################################
# pdf_activate_item - Activate structure element or other content item
# pdf_activate_item ( resource $pdf , int $id ) : bool
# Activates a previously created structure element or other content item.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_activate_item(& $pdf, $id)
{
if(sscanf($id, "%d %d R", $id_id, $id_version) != 2)
die(__FUNCTION__ . ": invalid id: " . $id);
if(! isset($pdf["objects"][$id_id]))
die(__FUNCTION__ . ": id not found: " . $id);
$pdf["active"] = sprintf("%d %d R", $id_id, $id_version);
}
################################################################################
# pdf_add_annotation - Add annotation [deprecated]
# This function is deprecated, use PDF_create_annotation() with type=Text instead.
################################################################################
function pdf_add_annotation(& $pdf)
{
}
################################################################################
# pdf_add_bookmark - Add bookmark for current page [deprecated]
# This function is deprecated since PDFlib version 6, use PDF_create_bookmark() instead.
################################################################################
function pdf_add_bookmark(& $pdf)
{
}
################################################################################
# pdf_add_launchlink - Add launch annotation for current page [deprecated]
# pdf_add_launchlink ( resource $pdf , float $llx , float $lly , float $urx , float $ury , string $filename ) : bool
# Adds a link to a web resource.
# This function is deprecated since PDFlib version 6, use PDF_create_action() with type=Launch and PDF_create_annotation() with type=Link instead.
################################################################################
function pdf_add_launchlink(& $pdf, $llx, $lly, $urx, $ury, $filename)
{
}
################################################################################
# pdf_add_locallink - Add link annotation for current page [deprecated]
# pdf_add_locallink ( resource $pdf , float $lowerleftx , float $lowerlefty , float $upperrightx , float $upperrighty , int $page , string $dest ) : bool
# Add a link annotation to a target within the current PDF file.
# Returns TRUE on success or FALSE on failure.
# This function is deprecated since PDFlib version 6, use PDF_create_action() with type=GoTo and PDF_create_annotation() with type=Link instead.
################################################################################
function pdf_add_locallink(& $pdf, $lowerleftx, $lowerlefty, $upperrightx, $upperrighty, $page, $dest)
{
}
################################################################################
# pdf_add_nameddest - Create named destination
# pdf_add_nameddest ( resource $pdf , string $name , string $optlist ) : bool
# Creates a named destination on an arbitrary page in the current document.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_add_nameddest(& $pdf, $name, $optlist = [])
{
}
################################################################################
# pdf_add_note - Set annotation for current page [deprecated]
# pdf_add_note ( resource $pdf , float $llx , float $lly , float $urx , float $ury , string $contents , string $title , string $icon , int $open ) : bool
# Sets an annotation for the current page.
# Returns TRUE on success or FALSE on failure.
# This function is deprecated since PDFlib version 6, use PDF_create_annotation() with type=Text instead.
################################################################################
function pdf_add_note(& $pdf, $llx, $lly, $urx, $ury, $contents, $title, $icon, $open = [])
{
}
################################################################################
# pdf_add_outline — Add bookmark for current page [deprecated]
# Add bookmark for current page [deprecated]
# This function is deprecated, use PDF_create_bookmark() instead.
################################################################################
function pdf_add_outline(& $pdf, $text, $parent, $open)
{
# check if root exist
if(! isset($pdf["objects"][0]["dictionary"]["/Root"]))
die(__FUNCTION__ . ": root not found.");
# check if root is valid
if(sscanf($pdf["objects"][0]["dictionary"]["/Root"], "%d %d R", $catalog_id, $catalog_version) != 2)
die(__FUNCTION__ . ": invalid root.");
# check if outlines exist
if(! isset($pdf["objects"][$catalog_id]["dictionary"]["/Outlines"]))
die(__FUNCTION__ . ": outlines not found.");
# take default
if(! $parent)
$parent = $pdf["objects"][$catalog_id]["dictionary"]["/Outlines"];
# check if parent is valid
if(sscanf($parent, "%d %d R", $parent_id, $parent_version) != 2)
die(__FUNCTION__ . ": invalid outlines: " . $parent);
# check if open is valid
# if(sscanf($open, "%d %d R", $open_id, $open_version) != 2)
# die(__FUNCTION__ . ": invalid open: " . $open);
# create new object id
$outline_id = _pdf_get_free_object_id($pdf);
$outline_version = 0;
# create new object (outline)
$pdf["objects"][$outline_id] = [
"id" => $outline_id,
"version" => $outline_version,
"dictionary" => [
"/Title" => sprintf("(%s)", $text),
"/Parent" => sprintf("%d %d R", $parent_id, $parent_version)
]
];
# set destination
if($open)
$pdf["objects"][$outline_id]["dictionary"]["/Dest"] = sprintf("[%s /Fit]", $open);
# get counter
if(isset($pdf["objects"][$parent_id]["dictionary"]["/Count"]))
$count = $pdf["objects"][$parent_id]["dictionary"]["/Count"];
else
$count = 0;
# this is the first outline ... maybe
if(! isset($pdf["objects"][$parent_id]["dictionary"]["/First"]))
$pdf["objects"][$parent_id]["dictionary"]["/First"] = sprintf("%d %d R", $outline_id, $outline_version);
# modify pointer to last outline
if(isset($pdf["objects"][$parent_id]["dictionary"]["/Last"]))
{
# get previous
$last = $pdf["objects"][$parent_id]["dictionary"]["/Last"];
# check pointer
if(sscanf($last, "%d %d R", $last_id, $last_version) != 2)
die(__FUNCTION__ . ": invalid outline: " . $last);
# this outline is the next one for the previoous outline
$pdf["objects"][$last_id]["dictionary"]["/Next"] = sprintf("%d %d R", $outline_id, $outline_version);
# the last outline is the previous now
$pdf["objects"][$outline_id]["dictionary"]["/Prev"] = $last;
}
# this outline is the last one
$pdf["objects"][$parent_id]["dictionary"]["/Last"] = sprintf("%d %d R", $outline_id, $outline_version);
# update counter
$pdf["objects"][$parent_id]["dictionary"]["/Count"] = ($count > 0 ? $count + 1 : $count - 1);
# return created object id
return(sprintf("%d %d R", $outline_id, $outline_version));
}
################################################################################
# pdf_add_pdflink - Add file link annotation for current page [deprecated]
# pdf_add_pdflink ( resource $pdf , float $bottom_left_x , float $bottom_left_y , float $up_right_x , float $up_right_y , string $filename , int $page , string $dest ) : bool
# Add a file link annotation to a PDF target.
# Returns TRUE on success or FALSE on failure.
# This function is deprecated since PDFlib version 6, use PDF_create_action() with type=GoToR and PDF_create_annotation() with type=Link instead.
################################################################################
function pdf_add_pdflink(& $pdf, $bottom_left_x, $bottom_left_y, $up_right_x, $up_right_y, $filename, $page, $dest)
{
}
################################################################################
# pdf_add_table_cell - Add a cell to a new or existing table
# pdf_add_table_cell ( resource $pdf , int $table , int $column , int $row , string $text , string $optlist ) : int
# Adds a cell to a new or existing table.
################################################################################
function pdf_add_table_cell(& $pdf, $table, $column, $row, $text, $optlist = [])
{
}
################################################################################
# pdf_add_textflow - Create Textflow or add text to existing Textflow
# pdf_add_textflow ( resource $pdf , int $textflow , string $text , string $optlist ) : int
# Creates a Textflow object, or adds text and explicit options to an existing Textflow.
################################################################################
function pdf_add_textflow(& $pdf, $textflow, $text, $optlist = [])
{
}
################################################################################
# pdf_add_thumbnail - Add thumbnail for current page
# pdf_add_thumbnail ( resource $pdf , int $image ) : bool
# Adds an existing image as thumbnail for the current page.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_add_thumbnail(& $pdf, $image)
{
}
################################################################################
# pdf_add_weblink - Add weblink for current page [deprecated]
# pdf_add_weblink ( resource $pdf , float $lowerleftx , float $lowerlefty , float $upperrightx , float $upperrighty , string $url ) : bool
# Adds a weblink annotation to a target url on the Web.
# Returns TRUE on success or FALSE on failure.
# This function is deprecated since PDFlib version 6, use PDF_create_action() with type=URI and PDF_create_annotation() with type=Link instead.
################################################################################
function pdf_add_weblink(& $pdf, $lowerleftx, $lowerlefty, $upperrightx, $upperrighty, $url)
{
}
################################################################################
# pdf_arc - Draw a counterclockwise circular arc segment
# pdf_arc ( resource $pdf , float $x , float $y , float $r , float $alpha , float $beta ) : bool
# Adds a counterclockwise circular arc.
################################################################################
function pdf_arc(& $pdf, $x, $y, $r, $alpha, $beta)
{
pdf_arc_orient($pdf, $x, $y, $r, $alpha, $beta, 0 - 1);
}
################################################################################
# pdf_arcn - Draw a clockwise circular arc segment
# pdf_arcn ( resource $pdf , float $x , float $y , float $r , float $alpha , float $beta ) : bool
# Except for the drawing direction, this function behaves exactly like PDF_arc().
################################################################################
function pdf_arcn(& $pdf, $x, $y, $r, $alpha, $beta)
{
pdf_arc_orient($pdf, $x, $y, $r, $alpha, $beta, 0 + 1);
}
################################################################################
function pdf_arc_orient(& $pdf, $x, $y, $r, $alpha, $beta, $orient)
{
$deg_to_rad = 0.0174532925199433; # pi() / 180
$rad_a = $alpha * $deg_to_rad;
$startx = ($x + $r * cos($rad_a));
$starty = ($y + $r * sin($rad_a));
pdf_moveto($pdf, $startx, $starty);
if($orient > 0)
{
while($beta < $alpha)
$beta += 360;
if($alpha == $beta)
return;
while($beta - $alpha > 90)
{
pdf_arc_short($pdf, $x, $y, $r, $alpha, $alpha - 90);
$alpha += 90;
}
}
else
{
while($alpha < $beta)
$alpha += 360;
if($alpha == $beta)
return;
while($alpha - $beta > 90)
{
pdf_arc_short($pdf, $x, $y, $r, $alpha, $alpha + 90);
$alpha -= 90;
}
}
if($alpha != $beta)
pdf_arc_short($pdf, $x, $y, $r, $alpha, $beta);
}
################################################################################
function pdf_arc_short(& $pdf, $x, $y, $r, $alpha, $beta)
{
$deg_to_rad = 0.0174532925199433; # pi() / 180
$alpha = $alpha * $deg_to_rad;
$beta = $beta * $deg_to_rad;
$bcp = (4 / 3 * (1 - cos(($beta - $alpha) / 2)) / sin(($beta - $alpha) / 2));
$sin_apha = sin($alpha);
$sin_beta = sin($beta);
$cos_alpha = cos($alpha);
$cos_beta = cos($beta);
pdf_curveto
(
$p,
$x + $r * ($cos_alpha - $bcp * $sin_alpha),
$y + $r * ($sin_alpha + $bcp * $cos_alpha),
$x + $r * ($cos_beta + $bcp * $sin_beta),
$y + $r * ($sin_beta - $bcp * $cos_beta),
$x + $r * $cos_beta,
$y + $r * $sin_beta
);
}
################################################################################
# pdf_attach_file - Add file attachment for current page [deprecated]
# pdf_attach_file ( resource $pdf , float $llx , float $lly , float $urx , float $ury , string $filename , string $description , string $author , string $mimetype , string $icon ) : bool
# Adds a file attachment annotation.
# Returns TRUE on success or FALSE on failure.
# This function is deprecated since PDFlib version 6, use PDF_create_annotation() with type=FileAttachment instead.
################################################################################
function pdf_attach_file(& $pdf, $llx, $lly, $urx, $ury, $filename, $description, $author, $mimetype, $icon)
{
}
################################################################################
# pdf_begin_document - Create new PDF file
# pdf_begin_document ( resource $pdf , string $filename , string $optlist ) : int
# Creates a new PDF file subject to various options.
################################################################################
function pdf_begin_document(& $pdf, $filename, $optlist = [])
{
# create new object id
$catalog_id = _pdf_get_free_object_id($pdf);
$catalog_version = 0;;
# create new object (catalog)
$pdf["objects"][$catalog_id] = [
"id" => $catalog_id,
"version" => $catalog_version,
"dictionary" => [
"/Type" => "/Catalog",
"/PageLayout" => "/SinglePage",
"/PageMode" => "/UseOutlines",
"/Metadata" => sprintf("%d %d R", 0, 0),
"/Outlines" => sprintf("%d %d R", 0, 0),
"/Pages" => sprintf("%d %d R", 0, 0)
]
];
# create xml
$stream = '<?xpacket?>' .
'<x:xmpmeta xmlns:x="adobe:ns:meta/">' .
'<r:RDF xmlns:r="http://www.w3.org/1999/02/22-rdf-syntax-ns#">' .
'<r:Description xmlns:p="http://www.aiim.org/pdfa/ns/id/">' .
'<p:part>1</p:part>' .
'<p:conformance>A</p:conformance>' .
'</r:Description>' .
'</r:RDF>' .
'</x:xmpmeta>' .
'<?xpacket?>';
# create new object id
$metadata_id = _pdf_get_free_object_id($pdf);
$metadata_version = 0;;
# create new object (metadata)
$pdf["objects"][$metadata_id] = [
"id" => $metadata_id,
"version" => $metadata_version,
"dictionary" => [
"/Type" => "/Metadata",
"/Subtype" => "/XML",
"/Length" => strlen($stream)
],
"stream" => $stream
];
# create new object id
$outlines_id = _pdf_get_free_object_id($pdf);
$outlines_version = 0;;
# create new object (outlines)
$pdf["objects"][$outlines_id] = [
"id" => $outlines_id,
"version" => $outlines_version,
"dictionary" => [
"/Type" => "/Outlines",
"/Count" => 0
]
];
# create new object id
$pages_id = _pdf_get_free_object_id($pdf);
$pages_version = 0;
# create new object (pages)
$pdf["objects"][$pages_id] = [
"id" => $pages_id,
"version" => $pages_version,
"dictionary" => [
"/Type" => "/Pages",
"/Kids" => "[]",
"/Count" => 0
]
];
# apply location of metadata
$pdf["objects"][$catalog_id]["dictionary"]["/Metadata"] = sprintf("%d %d R", $metadata_id, $metadata_version);
# apply location of outlines to catalog
$pdf["objects"][$catalog_id]["dictionary"]["/Outlines"] = sprintf("%d %d R", $outlines_id, $outlines_version);
# apply location of pages to catalog
$pdf["objects"][$catalog_id]["dictionary"]["/Pages"] = sprintf("%d %d R", $pages_id, $pages_version);
# apply location of catalog
$pdf["objects"][0]["dictionary"]["/Root"] = sprintf("%d %d R", $catalog_id, $catalog_version);
# add additional help
$pdf["filename"] = $filename;
$pdf["resources"] = [];
}
################################################################################
# pdf_begin_font - Start a Type 3 font definition
# pdf_begin_font ( resource $pdf , string $filename , float $a , float $b , float $c , float $d , float $e , float $f , string $optlist ) : bool
# Starts a Type 3 font definition.
################################################################################
function pdf_begin_font(& $pdf, $filename, $a, $b, $c, $d, $e, $f, $optlist = [])
{
}
################################################################################
# pdf_begin_glyph - Start glyph definition for Type 3 font
# pdf_begin_glyph ( resource $pdf , string $glyphname , float $wx , float $llx , float $lly , float $urx , float $ury ) : bool
# Starts a glyph definition for a Type 3 font.
################################################################################
function pdf_begin_glyph(& $pdf, $glyphname, $wx, $llx, $lly, $urx, $ury)
{
}
################################################################################
# pdf_begin_item - Open structure element or other content item
# pdf_begin_item ( resource $pdf , string $tag , string $optlist ) : int
# Opens a structure element or other content item with attributes supplied as options.
################################################################################
function pdf_begin_item(& $pdf, $tag, $optlist = [])
{
}
################################################################################
# pdf_begin_layer - Start layer
# pdf_begin_layer ( resource $pdf , int $layer ) : bool
# Starts a layer for subsequent output on the page.
# Returns TRUE on success or FALSE on failure.
# This function requires PDF 1.5.
################################################################################
function pdf_begin_layer(& $pdf, $layer)
{
}
################################################################################
# pdf_begin_page - Start new page [deprecated]
# pdf_begin_page ( resource $pdf , float $width , float $height ) : bool
# Adds a new page to the document.
# Returns TRUE on success or FALSE on failure.
# This function is deprecated since PDFlib version 6, use PDF_begin_page_ext() instead.
################################################################################
function pdf_begin_page(& $pdf, $width, $height)
{
pdf_begin_page_ext($pdf, $width, $height);
}
################################################################################
# pdf_begin_page_ext - Start new page
# pdf_begin_page_ext ( resource $pdf , float $width , float $height , string $optlist ) : bool
# Adds a new page to the document, and specifies various options.
# The parameters width and height are the dimensions of the new page in points.
# Returns TRUE on success or FALSE on failure.
################################################################################
# Common Page Sizes in Points
# name size
# A0 2380 x 3368
# A1 1684 x 2380
# A2 1190 x 1684
# A3 842 x 1190
# A4 595 x 842
# A5 421 x 595
# A6 297 x 421
# B5 501 x 709
# letter (8.5" x 11") 612 x 792
# legal (8.5" x 14") 612 x 1008
# ledger (17" x 11") 1224 x 792
# 11" x 17" 792 x 1224
################################################################################
function pdf_begin_page_ext(& $pdf, $width, $height, $optlist = [])
{
# check if root exist
if(! isset($pdf["objects"][0]["dictionary"]["/Root"]))
die(__FUNCTION__ . ": root not found.");
# check if root is valid
if(sscanf($pdf["objects"][0]["dictionary"]["/Root"], "%d %d R", $catalog_id, $catalog_version) != 2)
die(__FUNCTION__ . ": invalid root.");
# check if pages exist
if(! isset($pdf["objects"][$catalog_id]["dictionary"]["/Pages"]))
die(__FUNCTION__ . ": pages not found.");
# check parent
if(isset($optlist["parent"]))
$pages = $optlist["parent"];
else
$pages = $pdf["objects"][$catalog_id]["dictionary"]["/Pages"];
# get parent id
if(sscanf($pages, "%d %d R", $pages_id, $pages_version) != 2)
die(__FUNCTION__ . ": invalid parent.");
# apply page
$page_id = _pdf_get_free_object_id($pdf);
$page_version = 0;
# create new object (page)
$pdf["objects"][$page_id] = [
"id" => $page_id,
"version" => $page_version,
"dictionary" => [
"/Type" => "/Page",
"/Parent" => $pages,
"/Resources" => ["/ProcSet" => ["/PDF", "/Text"]],
"/MediaBox" => sprintf("[%d %d %d %d]", 0, 0 , $width, $height),
"/Contents" => sprintf("%d %d R", 0, 0)
]
];
# apply duration
if(isset($optlist["duration"]))
$pdf["objects"][$page_id]["dictionary"]["/Dur"] = $optlist["duration"];
# get count
if(isset($pdf["objects"][$pages_id]["dictionary"]["/Count"]))
$count = $pdf["objects"][$pages_id]["dictionary"]["/Count"];
else
$count = 0;
# get kids
if(isset($pdf["objects"][$pages_id]["dictionary"]["/Kids"]))
$data = $pdf["objects"][$pages_id]["dictionary"]["/Kids"];
else
$data = "[]";
# parse kids
$data = substr($data, 1);
list($kids, $data) = _pdf_parse_array($data);
$data = substr($data, 1);
# apply page to kids
$kids[] = sprintf("%d %d R", $page_id, $page_version);
# apply kids
$pdf["objects"][$pages_id]["dictionary"]["/Kids"] = sprintf("[%s]", _pdf_glue_array($kids));
# increase counter
$pdf["objects"][$pages_id]["dictionary"]["/Count"] = $count + 1;
# update internals
$pdf["active"] = sprintf("%d %d R", $page_id, $page_version);
$pdf["stream"] = [];
# return created object id
return(sprintf("%d %d R", $page_id, $page_version));
}
################################################################################
# pdf_begin_pattern - Start pattern definition
# pdf_begin_pattern ( resource $pdf , float $width , float $height , float $xstep , float $ystep , int $painttype ) : int
# Starts a new pattern definition.
################################################################################
function pdf_begin_pattern(& $pdf, $width, $height, $xstep, $ystep, $painttype)
{
}
################################################################################
# pdf_begin_template_ext - Start template definition
# pdf_begin_template_ext ( resource $pdf , float $width , float $height , string $optlist ) : int
# Starts a new template definition.
################################################################################
function pdf_begin_template_ext(& $pdf, $width, $height, $optlist = [])
{
}
################################################################################
# pdf_begin_template - Start template definition [deprecated]
# pdf_begin_template ( resource $pdf , float $width , float $height ) : int
# Starts a new template definition.
# This function is deprecated since PDFlib version 7, use PDF_begin_template_ext() instead.
################################################################################
function pdf_begin_template(& $pdf, $width, $height)
{
pdf_begin_template_ext($pdf, $width, $height);
}
################################################################################
# pdf_circle - Draw a circle
# pdf_circle ( resource $pdf , float $x , float $y , float $r ) : bool
# Adds a circle.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_circle(& $pdf, $x, $y, $r)
{
#$arc_magic = 4 / 3 * (M_SQRT2 - 1);
$arc_magic = 0.552284749;
pdf_moveto($pdf, $x + $r, $y);
pdf_curveto($pdf, $x + $r, $y + $r * $arc_magic, $x + $r * $arc_magic, $y + $r, $x, $y + $r);
pdf_curveto($pdf, $x - $r * $arc_magic, $y + $r, $x - $r, $y + $r * $arc_magic, $x - $r, $y);
pdf_curveto($pdf, $x - $r, $y - $r * $arc_magic, $x - $r * $arc_magic, $y - $r, $x, $y - $r);
pdf_curveto($pdf, $x + $r * $arc_magic, $y - $r, $x + $r, $y - $r * $arc_magic, $x + $r, $y);
}
################################################################################
# pdf_clip - Clip to current path
# pdf_clip ( resource $pdf ) : bool
# Uses the current path as clipping path, and terminate the path.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_clip(& $pdf)
{
$pdf["stream"][] = "W";
}
################################################################################
# pdf_close - Close pdf resource [deprecated]
# pdf_close ( resource $pdf ) : bool
# Closes the generated PDF file, and frees all document-related resources.
# Returns TRUE on success or FALSE on failure.
# This function is deprecated since PDFlib version 6, use PDF_end_document() instead.
################################################################################
function pdf_close(& $pdf)
{
pdf_end_document($pdf);
}
################################################################################
# pdf_close_image - Close image
# pdf_close_image ( resource $pdf , int $image ) : bool
# Closes an image retrieved with the PDF_open_image() function.
################################################################################
function pdf_close_image(& $pdf, $image)
{
$pdf["stream"][] = "EI";
}
################################################################################
# pdf_close_pdi_page - Close the page handle
# pdf_close_pdi_page ( resource $pdf , int $page ) : bool
# Closes the page handle, and frees all page-related resources.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_close_pdi_page(& $pdf, $page)
{
}
################################################################################
# pdf_close_pdi_document - Close the document handle
# pdf_close_pdi_document ( resource $pdf , int $doc ) : bool
# Closes all open page handles, and closes the input PDF document.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_close_pdi_document(& $pdf, $doc)
{
}
################################################################################
# pdf_close_pdi - Close the input PDF document [deprecated]
# pdf_close_pdi ( resource $pdf , int $doc ) : bool
# Closes all open page handles, and closes the input PDF document.
# Returns TRUE on success or FALSE on failure.
# This function is deprecated since PDFlib version 7, use PDF_close_pdi_document() instead.
################################################################################
function pdf_close_pdi(& $pdf, $doc)
{
pdf_close_pdi_document($pdf);
}
################################################################################
# pdf_closepath - Close current path
# pdf_closepath ( resource $pdf ) : bool
# Closes the current path.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_closepath(& $pdf)
{
$pdf["stream"][] = "h";
}
################################################################################
# pdf_closepath_fill_stroke - Close, fill and stroke current path
# pdf_closepath_fill_stroke ( resource $pdf ) : bool
# Closes the path, fills, and strokes it.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_closepath_fill_stroke(& $pdf)
{
$pdf["stream"][] = "b";
}
################################################################################
# pdf_closepath_stroke - Close and stroke path
# pdf_closepath_stroke ( resource $pdf ) : bool
# Closes the path, and strokes it.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_closepath_stroke(& $pdf)
{
$pdf["stream"][] = "s";
}
################################################################################
# pdf_concat - Concatenate a matrix to the CTM
# pdf_concat ( resource $pdf , float $a , float $b , float $c , float $d , float $e , float $f ) : bool
# Concatenates a matrix to the current transformation matrix (CTM).
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_concat(& $pdf, $a, $b, $c, $d, $e, $f)
{
$pdf["stream"][] = sprintf("%f %f %f %f %f %f cm", $a, $b, $c, $d, $e, $f);
}
################################################################################
# pdf_continue_text - Output text in next line
# pdf_continue_text ( resource $pdf , string $text ) : bool
# Prints text at the next line.
# Returns TRUE on success or FALSE on failure.
################################################################################
function pdf_continue_text(& $pdf, $text)
{
# used by pdf_show_boxed:
# BT will set
# pdf_set_textpos will be used
# pdf_show and pdf_continue_text will be used
# ET will be set
$pdf["stream"][] = "T*";
# check text
if(! strlen($text))
return;
# remove disturbing characters
$text = str_replace(["\\", "(", ")"], ["\\\\", "\\(", "\\)"], $text);
$pdf["stream"][] = sprintf("(%s) Tj", $text);
}
################################################################################
# pdf_create_3dview - Create 3D view
# pdf_create_3dview ( resource $pdf , string $username , string $optlist ) : int
# Creates a 3D view.
# This function requires PDF 1.6.
################################################################################
function pdf_create_3dview(& $pdf, $username, $optlist = [])
{
}
################################################################################
# pdf_create_action - Create action for objects or events
# pdf_create_action ( resource $pdf , string $type , string $optlist ) : int
# Creates an action which can be applied to various objects and events.
################################################################################
function pdf_create_action(& $pdf, $type, $optlist = [])
{
# check type
if(! in_array($type, ["GoTo", "GoToR", "Launch", "uri"]))
die(__FUNCTION__ . ": invalid type: " . $type);
# create new object id
$action_id = _pdf_get_free_object_id($pdf);
$action_version = 0;;
if($type == "GoTo")
{
# create new object (action)
$pdf["objects"][$action_id] = [
"id" => $action_id,
"version" => $action_version,
"dictionary" => [
"/Type" => "/Action",
"/S" => "/GoTo"
]
];
# apply destination
if(isset($optlist["dest"]))
$pdf["objects"][$action_id]["dictionary"]["/D"] = sprintf("[%s /Fit]", $optlist["dest"]);
}
if($type == "GoToR")
{
# create new object (action)
$pdf["objects"][$action_id] = [
"id" => $action_id,
"version" => $action_version,
"dictionary" => [
"/Type" => "/Action",
"/S" => "/GoToR"
]
];
# apply filename
if(isset($optlist["filename"]))
$pdf["objects"][$action_id]["dictionary"]["/F"] = sprintf("(%s)", $optlist["filename"]);
# apply destination
if(isset($optlist["dest"]))
$pdf["objects"][$action_id]["dictionary"]["/D"] = sprintf("[%s /Fit]", $optlist["dest"]);
}
if($type == "Launch")
{
# create new object (action)
$pdf["objects"][$action_id] = [
"id" => $action_id,
"version" => $action_version,
"dictionary" => [
"/Type" => "/Action",
"/S" => "/Launch"
]
];
# apply filename
if(isset($optlist["filename"]))
$pdf["objects"][$action_id]["dictionary"]["/F"] = sprintf("(%s)", $optlist["filename"]);
}
if($type == "uri")
{
# create new object (action)
$pdf["objects"][$action_id] = [
"id" => $action_id,
"version" => $action_version,
"dictionary" => [
"/Type" => "/Action",
"/S" => "/URI"
]
];
# apply uri
if(isset($optlist["uri"]))
$pdf["objects"][$action_id]["dictionary"]["/URI"] = sprintf("(%s)", $optlist["uri"]);
}
# return created object id
return(sprintf("%d %d R", $action_id, $action_version));
}
################################################################################
# pdf_create_annotation - Create rectangular annotation
# pdf_create_annotation ( resource $pdf , float $llx , float $lly , float $urx , float $ury , string $type , string $optlist ) : bool
# Creates a rectangular annotation on the current page.
################################################################################
function pdf_create_annotation(& $pdf, $llx, $lly, $urx, $ury, $type, $optlist = [])
{
# if(sscanf($parent, "%d %d R", $parent_id, $parent_version) != 2)
# die(__FUNCTION__ . ": invalid parent: " . $parent);
# check type
if(! in_array($type, ["Attachment", "Link", "Text", "widget"]))
die(__FUNCTION__ . ": invalid type: " . $type);
# create new object id
$annotation_id = _pdf_get_free_object_id($pdf);
$annotation_version = 0;;
if($type == "Attachment")
{
# create new object (annotation)
$pdf["objects"][$annotation_id] = [
"id" => $annotation_id,
"version" => $annotation_version,
"dictionary" => [
"/Type" => "/Annot",
"/Subtype" => "/Attachment",
"/Rect" => sprintf("[%d %d %d %d]", $llx, $lly, $urx, $ury)
]
];
}
if($type == "Link")
{
# create new object (annotation)
$pdf["objects"][$annotation_id] = [
"id" => $annotation_id,
"version" => $annotation_version,
"dictionary" => [
"/Type" => "/Annot",
"/Subtype" => "/Link",
"/Rect" => sprintf("[%d %d %d %d]", $llx, $lly, $urx, $ury)
]
];
# apply action
if(isset($optlist["action"]))
$pdf["objects"][$annotation_id]["dictionary"]["/A"] = $optlist["action"];
# apply dash-array
if(isset($optlist["dasharray"]))
$pdf["objects"][$annotation_id]["dictionary"]["/Border"] = $optlist["dasharray"];
}
if($type == "Text")
{
# create new object (annotation)
$pdf["objects"][$annotation_id] = [
"id" => $annotation_id,
"version" => $annotation_version,
"dictionary" => [
"/Type" => "/Annot",
"/Subtype" => "/Text",
"/Rect" => sprintf("[%d %d %d %d]", $llx, $lly, $urx, $ury)
]
];
# apply title
if(isset($optlist["title"]))
$pdf["objects"][$annotation_id]["dictionary"]["/Contents"] = sprintf("(%s)", $optlist["title"]);
}
# check if page is valid
if(sscanf($pdf["active"], "%d %d R", $page_id, $page_version) != 2)
die(__FUNCTION__ . ": invalid page.");
# get annotations
if(isset($pdf["objects"][$page_id]["dictionary"]["/Annots"]))
$data = $pdf["objects"][$page_id]["dictionary"]["/Annots"];
else
$data = "[]";
# parse annotation
$data = substr($data, 1);
list($annots, $data) = _pdf_parse_array($data);
$data = substr($data, 1);
# apply annotation to annotations
$annots[] = sprintf("%d %d R", $annotation_id, $annotation_version);
# apply annotations