I'm trying to write a code which adds hyperlinks to existing PDF document.
I've tried to do that by following PDF 32000 specification but seems that I'm missing something.
PDF viewer shows my link annotations and changes the mouse pointer to "finger" when hovering over them, but no URLs are shown, and clicking on them doesn't do anything.
PDF contents are shown below.
Page:
/Type/Page
/Parent 1 0 R
/MediaBox[0 0 594.75 841.5]
/Contents 7 0 R
/Resources 8 0 R
/Annots [5 0 R 6 0 R] % added by me
First annotation (for "Google", #5):
/Type /Annot
/Subtype /Link
/Rect [60.0 779.25 150.0 767.25]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/Subtype /URI
/URI (https://google.com)
>>
Second annotation (for "DuckDuckGo", #6):
/Type /Annot
/Subtype /Link
/Rect [342.0 694.5 432.0 682.5]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/Subtype /URI
/URI (https://duckduckgo.com)
>>
Content stream:
1 0 0 -1 36.0 841.5 cm
-100 Tz
q
/Font3 -9.0 Tf
0.0 0.0 0.0 rg
24.0 62.25 90.0 12.0 re W n
BT
1 0 0 1 0 0 Tm
24.75 69.75 Td
(Google) Tj
ET
Q
q
/Font3 -9.0 Tf
0.0 0.0 0.0 rg
306.0 147.0 90.0 12.0 re W n
BT
1 0 0 1 0 0 Tm
306.75 154.5 Td
(DuckDuckGo) Tj
ET
Q
This code produces the following result:
Generated PDF file is available here.
I took a look on the code the Microsoft Word produces for the links, and it looks pretty much the same (except minification and structural parent; this code works, but mine is not):
/Subtype/Link/Rect[ 69.75 697.51 113.16 720] /BS
<<
/W 0
>>
/F 4/A
<<
/Type/Action/S/URI/URI(https://google.com/)
>>
/StructParent 0
Any advice will be much appreciated.
Your action dictionary looks like this:
<<
/Type /Action
/Subtype /URI
/URI (https://google.com)
>>
If you lookup ISO 32000-1 section 12.6.2 Action Dictionaries, though, you'll see that the type of action is not the value of Subtype but instead of S:
S
name
(Required) The type of action that this dictionary describes; see Table 194 for specific values.
(ISO 32000-1 Table 193 – Entries common to all action dictionaries)
Thus, in your Action dictionary (not in the surrounding Annot dictionary, though!) replace /Subtype by /S:
/Type /Annot
/Subtype /Link %<------------- not here
/Rect [342.0 694.5 432.0 682.5]
/BS
<<
/W 2 % should be 0, set to 2 to be able to see the annotation's position
>>
/F 4
/A
<<
/Type /Action
/S /URI %<------------- but here
/URI (https://duckduckgo.com)
>>
You have to parse the page content and process all operators that affect text position in order to compute the exact position of Google and DuckDuckGo. Then place the link annotations at the computed positions.
Related
I see the following objects in a pdf file. It is clear that 16875 is the top /Pages and 16876 is a dictionary of all the link destinations in the pdf. I only see one object with the type /Catalog in the pdf.
Is it always the case that there is only one /Catalog object which contains a pointer to a top /Pages object and a pointer to an object of all the link destinations?
119038 0 obj
<</Type /Catalog
/Pages 16875 0 R
/Dests 16876 0 R
/MarkInfo <</Type /MarkInfo
/Marked true>>
/StructTreeRoot 16877 0 R>>
endobj
16876 0 obj
<</05_preface [11 0 R /XYZ 34.005386 763.99994 0]
/sec1 [339 0 R /XYZ 34.005386 400.67438 0]
/fn1 [339 0 R /XYZ 339.52963 268.5553 0]
...
16875 0 obj
<</Type /Pages
/Count 1603
/Kids [16871 0 R 16872 0 R 16873 0 R 16874 0 R]>>
endobj
Is it always the case that there is only one /Catalog object which contains a pointer to a top /Pages object and a pointer to an object of all the link destinations?
There can be an arbitrary number of dictionaries with type Catalog in a PDF. The one that is relevant is the one referenced as Root from the trailer.
Beware, though, the Dests only contain the named destinations, not necessarily all of them.
I'm trying to remove certain objects from a PDF file. The objects all look like this:
40 0 obj
<<
/PieceInfo
/Subtype /Form
/Resources
<<
/Font
<<
/Fm1 35 0 R
>>
>>
/Type /XObject
/BBox [0 -22.5 131.05 0]
/Length 601
/Matrix [1 0 0 1 0 0]
>>
stream
. . .
A bunch of compressed gibberish here
. . .
endstream
endobj
What I found to work without breaking the PDF document is deleting the stuff between obj, stream, and endstream.
Is there a way in sed or awk to look for lines containing /Form, and then deleting everything between the nearest obj above and stream below, and that stream and the endstream below it, so that the final result looks like this:
40 0 obj
stream
endstream
endobj
Given:
$ echo "$pdf"
40 0 obj
<<
/PieceInfo
/Subtype /Form
/Resources
<<
/Font
<<
/Fm1 35 0 R
>>
>>
/Type /XObject
/BBox [0 -22.5 131.05 0]
/Length 601
/Matrix [1 0 0 1 0 0]
>>
stream
. . .
A bunch of compressed gibberish here
. . .
endstream
endobj
You can use perl:
$ echo "$pdf" | perl -0777 -lne 'print "$1$2$3\n" if /(^.*(?<=\bobj)\s*\R)[\s\S]*?\/Form[\s\S]*?^(stream\s*^)[\s\S]*?^(endstream\s+endobj)/m'
40 0 obj
stream
endstream
endobj
Demo and explanation of regex
perl -0777 -pe 's/(?<=obj)[\s\S]+?\/Form[\s\S]+?\n(?=endstream)/\nstream\n/g' pdf
There are a LOT of ways this regex can backfire (The key issue is "obj" or "endstream" appearing midstream or those fields or "/Form" being missing). You'll need a full script for something production quality, in which case you would definitely need to "show your work" to get help. Also you may need to remove or change the \n before (?=endstream) for an actual PDF. I'm not familiar with the line end characters it uses.
The jist, as a glob, would be that it looks for obj*/Form*endstream, and then clobbers everything not in a lookaround (?[etc]), and it manually readds the stream line.
awk may also do the job,
awk '/[^end]obj/||/[end]*stream/{print;if(d==1){s=""}d=1;next}{s=s $0}END{print s}' pdf
Brief explanation,
/[^end]obj/||/[end]*stream/: locate the string 'obj', 'stream', and 'endstream'
If above string is existed in the line, print it and enable the flag d
If the d is already enabled, clear the buffer str
Print the str in the end
This might work for you (GNU sed):
sed -r '/\<obj\>/{n;:a;/\<endobj\>/!{N;ba};s/.*\<(stream)\>.*\<(endobj)\>/\1\n\2/}' file
Gather up the lines between obj and endobj and remove the parts either side of stream.
$ cat tst.awk
$NF == "endobj" {
print (obj ~ "/Form" ? "stream" ORS "endstream" : obj)
obj = ""
inObj = 0
}
inObj { obj = (obj == "" ? "" : obj ORS) $0 }
!inObj { print }
$NF == "obj" { inObj = 1 }
$ awk -f tst.awk file
40 0 obj
stream
endstream
endobj
I'm writing code to produce pdfs (from postscript of course), and I've tried to follow the spec as best I could. But imagemagick's identify says there's something wrong with my xref table.
Can anyone see where/what my problem is?
$ echo quit | gsnd -q pw.ps dancingmen.ps | identify -
**** Warning: An error occurred while reading an XREF table.
**** The file has been damaged. This may have been caused
**** by a problem while converting or transfering the file.
**** Ghostscript will attempt to recover the data.
**** This file had errors that were repaired or ignored.
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
-=>/tmp/magick-16940kBciKvHuOrD3 PBM 612x792 612x792+0+0 16-bit Bilevel Gray 61KB 0.000u 0:00.000
My pdf (made with ghostscript on Linux, single LF eols):
%PDF-1.3
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<< /Kids [ 3 0 R ]
/Type /Pages
/Count 1
>>
endobj
3 0 obj
<< /Contents [ 4 0 R ]
/MediaBox [ 0.0 0.0 612.0 792.0 ]
/Type /Page
/Parent 2 0 R
>>
endobj
4 0 obj
<< /Length 1287
>>
stream
2.0 4.0 m 2.0 3.9 l 2.05516 3.9 2.1 3.94484 2.1 4.0 c 2.1 4.05516 2.05516 4.1 2.0 4.1 c 1.94484 4.1 1.9 4.05516 1.9 4.0 c 1.9 3.94484 1.94484 3.9 2.0 3.9 c f 2.0 3.6 m 2.5 3.1 l S -2.0 3.6 m -1.5 3.1 l S 2.0 3.1 m 2.4 2.8 l 2.1 2.4 l 2.2 2.35 l S -2.0 3.1 m -1.7 2.6 l -1.5 2.8 l S 2.0 3.9 m 2.0 3.6 l 2.0 3.1 l S 3.0 4.0 m 3.0 3.9 l 3.05516 3.9 3.1 3.94484 3.1 4.0 c 3.1 4.05516 3.05516 4.1 3.0 4.1 c 2.94484 4.1 2.9 4.05516 2.9 4.0 c 2.9 3.94484 2.94484 3.9 3.0 3.9 c f 3.0 3.6 m 3.5 3.1 l S -3.0 3.6 m -2.5 4.1 l S 3.0 3.1 m 3.0 2.3 l 3.15 2.3 l S -3.0 3.1 m -3.0 2.3 l -2.85 2.3 l S 3.0 3.9 m 3.0 3.6 l 3.0 3.1 l S 4.0 4.0 m 4.0 3.9 l 4.05516 3.9 4.1 3.94484 4.1 4.0 c 4.1 4.05516 4.05516 4.1 4.0 4.1 c 3.94484 4.1 3.9 4.05516 3.9 4.0 c 3.9 3.94484 3.94484 3.9 4.0 3.9 c f 4.0 3.6 m 4.5 4.1 l S -4.0 3.6 m -3.5 4.1 l S 4.0 3.1 m 4.3 2.6 l 4.5 2.8 l S -4.0 3.1 m -3.7 2.6 l -3.5 2.8 l S 4.0 3.9 m 4.0 3.6 l 4.0 3.1 l S 5.0 4.0 m 5.0 3.9 l 5.05516 3.9 5.1 3.94484 5.1 4.0 c 5.1 4.05516 5.05516 4.1 5.0 4.1 c 4.94484 4.1 4.9 4.05516 4.9 4.0 c 4.9 3.94484 4.94484 3.9 5.0 3.9 c f 5.0 3.6 m 5.5 4.1 l 5.5 4.3 l 5.6 4.3 l 5.6 4.2 l 5.5 4.2 l S -5.0 3.6 m -4.5 3.1 l S 5.0 3.1 m 5.4 2.8 l 5.1 2.4 l 5.2 2.35 l S -5.0 3.1 m -4.6 2.8 l -4.9 2.4 l -4.8 2.35 l S 5.0 3.9 m 5.0 3.6 l 5.0 3.1 l S
endstream
endobj
xref
0 4
0000000000 65535 f
0000000010 00000 n
0000000063 00000 n
0000000127 00000 n
0000000234 00000 n
trailer
<<
/Root 1 0 R
/Size 4
>>
startxref
1581
%%EOF
For reference, this is the postscript drawing which is being converted.
Update: I've fixed several of the issues mentioned: missing xref keyword, %%EOF instead of $$EOF. Same error from identify, but chrome's viewer actually shows me an image (really small, in the lower left corner because I haven't dealt with graphics state yet).
link to file
link to newer file with single content stream
Output from ghostscript:
$ echo pstack quit | gsnd -q data/pw.ps data/dancingmen.ps | gsnd -sDEVICE=ps2write -dPDFDEBUG -dPDFSTOPONERROR -
GPL Ghostscript 9.18 (2015-10-05)
Copyright (C) 2015 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
**** Warning: An error occurred while reading an XREF table.
**** The file has been damaged. This may have been caused
**** by a problem while converting or transfering the file.
**** Ghostscript will attempt to recover the data.
<<
/Root 1 0 R
/Size 4 >>
%Resolving: [1 0]
<<
/Type /Catalog /Pages 2 0 R
>>
endobj
%Resolving: [2 0]
<<
/Kids [
3 0 R
]
/Type /Pages /Count 1 >>
endobj
%Resolving: [3 0]
<<
/Contents [
4 0 R
]
/MediaBox [
0.0 0.0 612.0 792.0 ]
/Type /Page /Parent 2 0 R
>>
endobj
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [1 0]
%Resolving: [1 0]
%Resolving: [1 0]
%Resolving: [1 0]
%Resolving: [2 0]
Processing pages 1 through 1.
Page 1
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [3 0]
%Resolving: [3 0]
%Resolving: [3 0]
%Resolving: [3 0]
%Resolving: [3 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [1 0]
%Resolving: [2 0]
%Resolving: [4 0]
<<
/Length 1288 >>
stream
%FilePosition: 270
endobj
2.0 4.0 m
2.0 3.9 l
2.05516 3.9 2.1 3.94484 2.1 4.0 c
2.1 4.05516 2.05516 4.1 2.0 4.1 c
1.94484 4.1 1.9 4.05516 1.9 4.0 c
1.9 3.94484 1.94484 3.9 2.0 3.9 c
f
2.0 3.6 m
2.5 3.1 l
S
-2.0 3.6 m
-1.5 3.1 l
S
2.0 3.1 m
2.4 2.8 l
2.1 2.4 l
2.2 2.35 l
S
-2.0 3.1 m
-1.7 2.6 l
-1.5 2.8 l
S
2.0 3.9 m
2.0 3.6 l
2.0 3.1 l
S
3.0 4.0 m
3.0 3.9 l
3.05516 3.9 3.1 3.94484 3.1 4.0 c
3.1 4.05516 3.05516 4.1 3.0 4.1 c
2.94484 4.1 2.9 4.05516 2.9 4.0 c
2.9 3.94484 2.94484 3.9 3.0 3.9 c
f
3.0 3.6 m
3.5 3.1 l
S
-3.0 3.6 m
-2.5 4.1 l
S
3.0 3.1 m
3.0 2.3 l
3.15 2.3 l
S
-3.0 3.1 m
-3.0 2.3 l
-2.85 2.3 l
S
3.0 3.9 m
3.0 3.6 l
3.0 3.1 l
S
4.0 4.0 m
4.0 3.9 l
4.05516 3.9 4.1 3.94484 4.1 4.0 c
4.1 4.05516 4.05516 4.1 4.0 4.1 c
3.94484 4.1 3.9 4.05516 3.9 4.0 c
3.9 3.94484 3.94484 3.9 4.0 3.9 c
f
4.0 3.6 m
4.5 4.1 l
S
-4.0 3.6 m
-3.5 4.1 l
S
4.0 3.1 m
4.3 2.6 l
4.5 2.8 l
S
-4.0 3.1 m
-3.7 2.6 l
-3.5 2.8 l
S
4.0 3.9 m
4.0 3.6 l
4.0 3.1 l
S
5.0 4.0 m
5.0 3.9 l
5.05516 3.9 5.1 3.94484 5.1 4.0 c
5.1 4.05516 5.05516 4.1 5.0 4.1 c
4.94484 4.1 4.9 4.05516 4.9 4.0 c
4.9 3.94484 4.94484 3.9 5.0 3.9 c
f
5.0 3.6 m
5.5 4.1 l
5.5 4.3 l
5.6 4.3 l
5.6 4.2 l
5.5 4.2 l
S
-5.0 3.6 m
-4.5 3.1 l
S
5.0 3.1 m
5.4 2.8 l
5.1 2.4 l
5.2 2.35 l
S
-5.0 3.1 m
-4.6 2.8 l
-4.9 2.4 l
-4.8 2.35 l
S
5.0 3.9 m
5.0 3.6 l
5.0 3.1 l
S
**** This file had errors that were repaired or ignored.
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
%Resolving: [2 0]
%Resolving: [1 0]
Update: Sigh. I suppose it's best if I show the code. This program is intended to hook into certain drawing operators of postscript and capture paths and produce a pdf file of the contents. I'm ignoring the quality of the output, in particular transformation matrices, for now.
/prompt {} def
<<
/.create-pdf-data { % called at start
install-operator-overrides
}
/.create-pdf-page { % called at showpage
1 /PageNumber +=
<< /Type /Page
/Parent pdf-object-names /Pages get create-ref
/MediaBox [gsave newpath clippath pathbbox grestore]
/Contents []
>>
current-page-name dup 3 1 roll create-object
pdf-object-names exch get create-ref add-to-pages-kids
[ display-list {
exch pop
create-content-stream
} for-each ]
{ ( ) exch strcat strcat } reduce
add-content-to-page
}
/current-page-name {
(Page) PageNumber as-string strcat
}
/current-page {
pdf-objects pdf-object-names current-page-name get get
}
/.output-pdf { % called at quit
/OutputFileName where { pop OutputFileName }{ (%stdout) } ifelse
(w) file write-pdf
pstack
}
/operator-overrides <<
%/start .create-pdf-data
/stroke ({ mark-path /S cvx ] display //super//call })
/fill ({ mark-path /f cvx ] display //super//call })
/showpage ({ .create-pdf-page //super//call })
/quit ({ .output-pdf //super//call })
>>
/install-operator-overrides {
operator-overrides {
1 index load
dup /super exch def
type /arraytype eq { /exec load }{ /dummyproc cvx } ifelse
/call exch def
cvx exec userdict 3 1 roll put
} forall
userdict /dummyproc {} put
}
/PageNumber 0
/+= { dup load 3 2 roll add store }
/write-pdf {
/f exch def
(1.3) write-header
write-body
write-xref-table
write-trailer
}
/pdf-output-file-position 0
/write-header {
/pdf-output-file-position 0 store
(%PDF-) .w .w \n \n
}
/write-body {
write-objects-and-save-positions
}
/write-objects-and-save-positions {
pdf-objects {
1 index save-position
write-object
} for-each
}
/write-xref-table {
(xref) .w \n
pdf-output-file-position /xref-position exch def
(0 ) .w pdf-object-positions length 1 sub .n \n
0 format-10 .w ( 65535 f ) .w \n
pdf-object-positions {
write-xref-table-row
} for-each
}
/write-xref-table-row {
exch pop format-10 .w
( 00000 n ) .w \n
}
/format-10-string 20 string
/format-10 {
format-10-string cvs
(0000000000) 0 10 3 index length sub getinterval
exch strcat
}
/write-trailer {
(trailer) .w \n
(<<) .w \n
( /Root 1 0 R) .w \n
( /Size ) .w pdf-objects length 1 sub .n \n
(>>) .w \n
(startxref) .w \n
xref-position .n \n
(%%EOF) .w \n
}
/create-content-stream {
to-string-with-spaces
%dup length ==only ( ) print ==
}
/write-object {
exch .n ( 0 obj) .w \n
dup write-dict
pdf-streams exch 2 copy known { write-stream }{ pop pop } ifelse
(endobj) .w \n \n
}
/write-stream {
(stream) .w \n
get .w \n
(endstream) .w \n
}
/write-dict {
(<< ) .w
{ exch write-thing write-thing \n } forall
(>> ) .w \n
}
/write-thing {
+is-ref { write-ref }{
+is-name { write-name }{
+is-array { write-array }{
+is-null { pop (null ) .w }{
.n ( ) .w
} ifelse } ifelse } ifelse } ifelse
}
/write-ref {
ref .n ( 0 R ) .w
}
/write-name {
dup xcheck not { (/) .w } if
.n ( ) .w
}
/write-array {
([ ) .w
{ write-thing } forall
(] ) .w
}
/+is-ref { dup is-ref }
/+is-name { dup is-name }
/+is-array { dup is-array }
/+is-null { dup is-null }
/is-string { type /stringtype eq }
/is-array { type /arraytype eq }
/is-name { type /nametype eq }
/is-null { type /nulltype eq }
/is-ref { +is-name { is-ref-format }{ pop false } ifelse }
/is-ref-format { ref-check-string cvs 0 1 getinterval (&) eq }
/ref-check-string 20 string
/ref { 10 string cvs rest cvi }
/create-ref { (&) exch 10 string cvs strcat cvn }
/mark-path { [ { /m } { /l } { /c } { /h } pathforall }
/display { add-to-display-list }
/display-list <<
0 null
>>
/add-to-display-list { display-list dup 3 1 roll length exch put }
/clear-display-list { /display-list << 0 null >> store }
/pdf-objects << % integer keys
0 null
1 << /Type /Catalog /Pages /&2 >>
2 << /Type /Pages /Kids [] /Count 0 >>
>>
/pdf-object-names << % integer values
/Catalog 1
/Pages 2
>>
/pdf-object-positions << % integer keys
0 null
>>
/pdf-streams <<
>>
/create-object { % dict name
exch pdf-objects dup length 3 2 roll put
pdf-object-names exch pdf-objects length 1 sub put
}
/object { % name -> dict
pdf-object-names exch get pdf-objects exch get
}
/save-position {
pdf-object-positions exch pdf-output-file-position put
}
/Pages {
pdf-objects pdf-object-names /Pages get get
}
/add-content-to-page {
<<
/Length 2 index length 1 add
>> dup 3 2 roll pdf-streams 3 1 roll put
/current-content create-object
pdf-object-names /current-content get create-ref
current-page /Contents 2 copy get [ exch {}forall counttomark 4 add -1 roll ] put
}
/add-to-pages-kids { % ref
Pages /Kids 2 copy get [ exch {}forall counttomark 4 add -1 roll ] put
Pages /Count 2 copy get 1 add put
}
/.w { f exch dup length /pdf-output-file-position += writestring }
/.n { dup is-string not { .n-string cvs } if .w }
/.n-string 100 string
/\n { (\n) .w }
/to-string-with-spaces { {as-string} map {( ) exch strcat strcat} reduce }
/map { 1 index xcheck 3 1 roll [ 3 1 roll forall ] exch { cvx } if }
/reduce { exch dup first exch rest 3 -1 roll forall }
/first { 0 get }
/rest { 1 1 index length 1 sub getinterval }
/as-string { 20 string cvs dup length 13 gt { 0 7 getinterval } if }
/strcat { 2 copy length exch length add string dup 4 2 roll
3 copy pop 0 exch putinterval exch length exch putinterval }
/for-each { % dict proc key(int) value *proc*
1 1 3 index length 1 sub % d p 1 1 lim
[ 6 5 roll % p 1 1 lim [ d
1 /index cvx /get cvx % p 1 1 lim [ d 1 index get
9 8 roll /exec cvx ] cvx % 1 1 lim { d 1 index get p exec }
for
}
>>
{ dup {
dup type /arraytype ne {
def
}{ % Dict name proc
[ 3 index /begin cvx
3 -1 roll {} forall
/end cvx
] cvx
def
} ifelse
} forall pop
} pop
begin
.create-pdf-data
Sigh, ran out of space in the comments again....
It would help to put the file somewhere, rather than pasting it. PDF files are binary and length calculations depend on things like CR/LF pairs, meaning that each /Length could potentially be incorrect and its not possible to tell from looking at the pasted file.
Similarly the xref table offsets could be incorrect. In fact the offset for entry 1 looks incorrect, even assuming LF EOLs, but its not possible to be certain from the pasted file.
Note the error message is from Ghostscript (which IM uses to deal with PDF files). You would probably get more information if you just fed the PDF file to Ghostscript in the first place. You could also try setting -DPDFDEBUG and -dPDFSTOPONERROR, the combination will print out which object GS is dealing with and what it thinks is the problem (if there's a PostScript error). Other PDF problems usually send some kind of back-channel output.
Notice that the Ghostscript message references the xref table as the problem:
**** Warning: An error occurred while reading an XREF table.
So I suspect your xref table is incorrect (also see below, object 0).
Non-breaking, but not best practice:
xref entry 0, the head of the linked list of free objects, has an offset of 0000000028 should be 0.
Your file seems to end $$EOF instead of %%EOF.
Its normal practice to place binary in a comment on line 2 in order to force applications to treat the file as binary when transmitting
Better to elide the Resources dictionary than use a null object, its smaller.
Similarly, better to have a single Contents stream (despite recent Adobe engines producing multiple streams) again because its smaller.
Obviously this is an early work in progress, I'm sure you will deal with these in time.
If you'll post the actual PDF file somewhere I can take a look.
[edit]
So the first problem is that the xref table subsection is incorrect. The subsection starts with 2 numbers, the initial index, and the number of entries in the table. The xref table has 5 entries starting from index 0 and going up to index 4. The subsection says
0 4
Correcting that to 0 5 leads us to the next problem, the Size entry in the trailer dictionary is 4, and should be 5.
But Ghostscript still complains.
The final problem is that the startxref offset is incorrect. Currently this is:
startxref
1581
But the actual byte offset of the 'xref' keyword is byte 1576.
If I correct all 3 of these problems then Ghostscript opens the file without complaint. It already did render the content of course (very tiny because there's no CTM operations) but now it doesn't have to fix the file.
I am looking for a way to draw a dot (or small circle) that is an internal "annotation" link to a popup box in a PDF using specification 1.7...
TCPDF does this internally, with code like... see: https://tcpdf.org/examples/example_036/
2 0 obj
<< /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 3 0 R /F2 4 0 R /F3 6 0 R >> /XObject << /XT5 5 0 R /I0 11 0 R >> >>
endobj
7 0 obj
<</Type /Annot /Subtype /Text /Rect [235.275591 737.008110 263.622047 765.354567] /Contents (þÿ
I am looking for a solution that does this directly. That is, explaining how the PDF language creates the trigger object (e.g., the circle), and the annotation object (e.g., the popup box), and how the internal linking works.
I want to do is to create the pdf report using jsreport with web api. All I have got is the response from jsreport server as the pdf content in the following format:
%PDF-1.4 1 0 obj << /Title (þÿ) /Creator (þÿ) /Producer (þÿQt 4.8.4 \(C\) 2011 Nokia Corporation and/or its subsidiary\(-ies\)) /CreationDate (D:20151118135420+05'45') >> endobj 3 0 obj << /Type /ExtGState /SA true /SM 0.02 /ca 1.0 /CA 1.0 /AIS false /SMask /None>> endobj 4 0 obj [/Pattern /DeviceRGB] endobj 5 0 obj << /Type /Page /Parent 2 0 R /Contents 8 0 R /Resources 10 0 R /Annots 11 0 R /MediaBox [0 0 595 842] >> endobj 10 0 obj << /ColorSpace << /PCSp 4 0 R /CSp /DeviceRGB /CSpg /DeviceGray >> /ExtGState << /GSa 3 0 R >> /Pattern << >> /Font << /F6 6 0 R /F7 7 0 R >> /XObject << >> >> endobj 11 0 obj [ ] endobj 8 0 obj << /Length 9 0 R /Filter /FlateDecode >> stream xÕOYrÄïý)úl`4,²Ù$ÃÀF2àÁðÁðÁõÚXx÷à¯ïÈî.Uvñ,;ÀÎ+_½?ù2###üÇßþýþ?ÿvÿãûßþçþ÷Ó¿ßÿv7ܯþùa¸_ïïwûíýïÜ}¾ÿ|÷ëݯOÿýåßïVïvÛÕñ?_ÿò·þa=|ùÒósåo¿ÿõîÇã/Þ?ùíý??ý¯ÿ»_ßÿÓÓþrÿ¯ÿöô¯?~çø?î¶ý»Ãá°ßýà¿Ç¬Þa·Ú®OÿÇ*üùøÿºû¿»ÿë×A?®VÍÃÃf{ôøÏß;èÏ/_¾þ3þjò·¿üûÃîþé³ÿý»?a¯_ýùÌþt÷ãÇÇûÍãý§?ß¿ì/ÿúôÇÝÕ8ÜúÓýß?-ùð÷þr·þ2Ó'ë¯ ¯l¾~r8óÁÃñ;¯l¿~²ýàqúÁîøËÃë'ûãSFç0ý¡e}æ¹Êw~>þ³cóùÒû0U¿$¾õücx͸*Ǧ"þR|ñøSáÁÃöÑpÚÁÚ3S<·q>|z:Ü=góq½Îa=¡ÙO¾éÙÌ÷xzþn½{wü£á&=ì eôù7÷Êîaÿnûõ&ùzLÿ8þVòr1½Î÷Ü1ßA¹jôpß8ÃñòX¯ÎÙ©øIÂVhÖ-Ìë6l§V(~ºòv©xÅÏßq±ó?ã0-25$O/ò¸Únvô³åý±`?¾g¨ß{׳ÇqøÖ Fàiï.Ô£KµîÀuÜßõg6ñh áöO ¿ó~zZãw׿ó|2G¿´?ñÓáâe-ýtxÄ/Ñ »ðW>ÌÃá;ûðNa®ÂªÄÕ_Kg/l<Þ!á+qüq%o;¦£ ?ésÏâÍß1LÌKXÿ÷a8ñ¥âÄ©»f:oøãütxÞ°iOãùáñáùç>]û~6<¿²îãv;ãOaöyûd5hÿ8Q ×ÓYÛO83æ v.ýÃêì¼Æ1>LmDxÞ¾Ï ¯µ[§ÿ½Ú<ÿÜ05Zëéµñ&âh-}g7ýà¸à#ñð¶9-=#\!ÇӹŠNð¹ ãøHã8ù¿#Ó6L¿~6ÌizÇté¯Ä>#þðаcÂ8Âò:sgÞîOø°´Ra×I =üJxhøÙ°Ó}ùì¸lôþøå¤Â)~wUj82áð0Í<ôéF ç0qÊ·åSÖ²þºqù§s8v{¼ã¬ðÚ3óÓtìü³áí¦Ï`Û0Ìa§²nª8ôðaµÑü 7Wõ`Aø]²V¨Ù Zof7 n¥8SÓg:¼v8äaø8"½Õ§%-|c½qíf0ЧP%àç<¸º_ôÆË -¦£~'C¸Ó¾ sX¿f0¬CÝrƾeâ#=N{8aË S¼l3nݺ?wHXÌ°1îGr·-z,GQ¹ùõ9G÷#~%ÌXp8è ËÀ±wX0|²KÃ{½BËýÆNð%^GpÙp(¡C°CÅK3sË6{hÙÝì/Ca¦Âé <³#_ûq5~ïõöîµ éH}Kù¸ÃÏ^ÉH0I}ÞS a°§Î`æw +<w<0=#°(o*üÄyoÇþ°!]oÂërÀQ_aÓu(ñ³Sw qû c«#Ø×_ÆVRøú$QKÖìÊÐÉfoðq3ûWkÁùV4Dçñ+xP,y !ELè`gÌçâï²WNßWÕU°ðAø ÁÄIÅ=®.ï¡°v[:Ë×ù%ì/?2ÇN` ðëò3ØáóÀ§.<#¸Zg&ìÁãtî¯Øv!ÁÃç!ø ìr`¸3-ubdxb,2½uã¼!ïr¨LHN|}¾|³Ï·öù TÂMä+]ýàì7>=r çâ½ÀN_=XfO§×+HXæ?µämä 5Ï0a`ÁmâÚ:Èã³,áÔ¯¸ø³* å ñfT(í%lÙx¤LæIõÁÕ·ÄÚOÜdÃÌ.$G&(|Ä8fd7áu Øá µøÎ>ÉqzµõKYA©:þ= 4ÊÄÙ)XrÓÿ®=ØëðÄÐ Ü\à¬ÀLg_]pøêì° ×æò ÍvøÆXçLÀùUî²±HÕcÁDØʶ`h¾.#bJº#n è ÖÆ$üÞÿ<elÕy3;*Ð/ÔFÔ«ú\kÖ/;÷ú¼ØÌ^Îùfÿð¥àâ ·_Â#qÊNÀæ8:g$2 ½G¯^DêHXgJ,õ[J ¾»Ä[®Ûì'T´õåéáðÅ#NdàùípÊùeNã1öÎøÚH%Ñé4^H×8¬&ÿ ;\·â(da÷v½ÒAÆ,b_ Ù5ztv°Â;;%w.S&×¢8!j·ÃhO õÌdT¯T'/Ëà qnQÿt Àm·[ 7B&ØÔ]âõTéMqQG ±¬® $t OaIÔ}Ä55ºZïX¬+g3¦9¹Ç®cRk dh*ËÜ<c3g½Ù }-±àâº2.Öj¡¥2©²ïI¥R½'5Bi¸!{<7!ë['ê ø¬B å<§ \9N¸¿î£¡.nw2'¸¥ú5äëæ *ðòÕÙ1ÍÅ0lAÉáX¼l(ÄoÑá#Âuæ+kØA3 ƺ<váó^gä*.S&à¹KTÍ®÷óÈ"ë²r_¿:«À¼ ¢%ZâKßRà'x0uÃa^)U¶²·'Töð¶s¼ZýoTx,962¼tB®¬Nsã0Ð/" 5o½à0-$ÜÆ·£rZMUËù*IÐ ëØ ×w ÂÝæD¯!Æ°¶É+qB^UV¹^è'D#'Ú1G|92`ç,-DaÆp®#Ç¥P(Æ7Ñ;ÅÁCGôx]Ci¸Ò¢Cgo¾ !Á¯/ì¹Øÿ¿Loadn;ág5k¶mT{BëË!ú¿#v¹.MÍüH£Rû³¬%!¸K°7¯*ÆÁãfÓjàM%Z8ÔqqÁTÁjOláþ24 ¨N)`¦ìX íJºL^|¥ Å¥p{7l6;cB©·EáÑ¥DTÝ77ÃKa¤B&5<#° ³Ñzâ¶qÄ7wëõØ3XNV=àHë$LcòÜ2K°u$v6Lov÷f)ù½ ³ñ;*¸Âw5%Gõ]îר#¢ô IëßD¥ÙD(ǯþKsç8§X:& 3·# HJV%«wtKÔ9*/rð/dD.õ¼¾LåPÝ^üRG¾4sæÌ5;|³ýÍB9¼¦2H¼Må¡ WwWeH9 ݨX`8±RÎüz~=F¤ã&g,Õ5·c²Î4AS)ñv¥Fô,´oÂ÷2*oÍIx{uf Å·¼W[_$ôÊVøb|È=`ÓÈÈ&MïLTa3°vCdÝ®fçòµuFÝHlêh)9jÇUd9[T¸¹ZÄã9*SùÆvܼÚBp¸¸°E¿¡¤#êNííØTd0/Áª ` CiÇÞ ¨V ³E !ø2¼ØBwKõ õ*#\õLµ45VâEf§;BÌú5 $/âuÔºîºD~4[ÿ×ës>¼$¨TPúÀX£½R<=[|?ô,O>ëyKÉXçá|ù¡ ¹ å*hô0ÞBßâ[༠<:V¹ñ¦p ¡ZbáͶ(IXÈøÒC## Heading
Now I just want to create the pdf file with this content (get as report.content) and save on the server (In Report folder of the project).
jsreport server returned you pdf document in string. simply you have to write this string into file. if you are using php you can do it by
file_put_contents()
sample demo to represent what is returned from jsreport server
// Read the input pdf file in string
$stringPdf = file_get_contents("input.pdf");
echo $stringPdf;
// write string file in pdf
file_put_contents("output.pdf",$stringPdf);
?>