What's wrong with my pdf writer? - pdf
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.
Related
MDF to VTK Converting using AWK
I am a beginner so sorry if this has been covered before,but I can't seem to find exactly what I need to solve my problem. I am trying to write an AWK "script" that can convert an MDF(Mesh Definition File) as input into a (VALID) VTK file as output. I have a sample MDF file that looks like this : TITLE "1" NMESHPOINTS 4 NNODES 4 NELEMENTS_TRIANG1 2 TIMESTEP 0.00001 NINTERNAL_TIMESTEPS 1000 NEXTERNAL_TIMESTEPS 100 DAMPING_FACTOR 0.01 MESHPOINT_COORDINATES 1 0.0 0.0 0.0 2 1.0 0.0 0.0 3 1.0 1.0 0.0 4 0.0 1.0 0.0 NODES_TRIANG1 1 1 2 3 2 1 3 4 And I want to make a valid VTK file from this input. Here is how the output should look like: # vtk DataFile Version 1.0 2D Unstructured Grid ASCII DATASET UNSTRUCTURED_GRID POINTS 4 float 0.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 CELLS 2 8 3 0 1 2 3 0 2 3 CELL_TYPES 2 5 5 I tried to make a picture how the mappings works I hope it explains some of them. To make it a bit easier for this specific example let's say we only want to work with triangles. Sadly I dont have the same file as VTK and MDF too, I tried to manualy write one. Is there any way to do this with AWK? Any help will be much appreciated!!
Excellent diagram showing the input -> output mapping! Made it extremely easy to write this: $ cat tst.awk $1 ~ /^[[:alpha:]]/ { f[$1] = $2 } !NF { block = "" } $1 == "MESHPOINT_COORDINATES" { block = $1 print "# vtk DataFile Version 1.0" print "2D Unstructured Grid" print "ASCII" print "" print "DATASET UNSTRUCTURED_GRID" printf "POINTS %d float\n", f["NMESHPOINTS"] next } block == "MESHPOINT_COORDINATES" { $1 = "" sub(/^[[:space:]]+/,"") print } $1 == "NODES_TRIANG1" { block = $1 printf "\nCELLS %d %d\n", f["NELEMENTS_TRIANG1"], f["NELEMENTS_TRIANG1"] * 4 next } block == "NODES_TRIANG1" { printf "%s", 3 for (i=2; i<=NF; i++) { printf " %s", $i - 1 } print "" nlines++ } END { printf "\nCELL_TYPES %d\n", nlines for (i=1; i<=nlines; i++) { print 5 } } . $ awk -f tst.awk file.mdf # vtk DataFile Version 1.0 2D Unstructured Grid ASCII DATASET UNSTRUCTURED_GRID POINTS 4 float 0.0 0.0 0.0 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 0.0 CELLS 2 8 3 0 1 2 3 0 2 3 CELL_TYPES 2 5 5 Normally we only answer questions where the poster has attempted to solve it themselves first but you put enough effort into creating the example and describing the mapping that IMHO you deserve help with a solution so - see the above, try to figure out how it's working yourself (add "prints", check the man page, etc.) and then post a new question if you have any specific questions about it.
Format time from alphanumeric to numeric
I have a text file: ifile.txt x y z t value 1 1 5 01hr01Jan2018 3 1 1 5 02hr01Jan2018 3.1 1 1 5 03hr01Jan2018 3.2 1 3.4 3 01hr01Jan2018 4.1 1 3.4 3 02hr01Jan2018 6.1 1 3.4 3 03hr01Jan2018 1.1 1 4.2 6 01hr01Jan2018 6.33 1 4.2 6 02hr01Jan2018 8.33 1 4.2 6 03hr01Jan2018 5.33 3.4 1 2 01hr01Jan2018 3.5 3.4 1 2 02hr01Jan2018 5.65 3.4 1 2 03hr01Jan2018 3.66 3.4 3.4 4 01hr01Jan2018 6.32 3.4 3.4 4 02hr01Jan2018 9.32 3.4 3.4 4 03hr01Jan2018 12.32 3.4 4.2 8.1 01hr01Jan2018 7.43 3.4 4.2 8.1 02hr01Jan2018 7.93 3.4 4.2 8.1 03hr01Jan2018 5.43 4.2 1 3.4 01hr01Jan2018 6.12 4.2 1 3.4 02hr01Jan2018 7.15 4.2 1 3.4 03hr01Jan2018 9.12 4.2 3.4 5.5 01hr01Jan2018 2.2 4.2 3.4 5.5 02hr01Jan2018 3.42 4.2 3.4 5.5 03hr01Jan2018 3.21 4.2 4.2 6.2 01hr01Jan2018 1.3 4.2 4.2 6.2 02hr01Jan2018 3.4 4.2 4.2 6.2 03hr01Jan2018 1 Explanation: Each coordinate (x,y) has a z-value and three time values. The spaces are not tabs. They are sequence of spaces. I would like to format the t-column from alphanumeric to numeric and then convert to a csv file. My expected output is as: ofile.txt x,y,z,201801010100,201801010200,201801010300 1,1,5,3,3.1,3.2 1,3.4,3,4.1,6.1,1.1 1,4.2,6,6.33,8.33,5.33 3.4,1,2,3.5,5.65,3.66 3.4,3.4,4,6.32,9.32,12.32 3.4,4.2,8.1,7.43,7.93,5.43 4.2,1,3.4,6.12,7.15,9.12 4.2,3.4,5.5,2.2,3.42,3.21 4.2,4.2,6.2,1.3,3.4,1 The desire time format is replaced with YYYYMMDDHHMin. I had asked part of this question previously. Please see Format and then convert txt to csv using shell script and awk. However I can't able to change the time format within the following script. awk -v OFS=, '{k=$1 OFS $2 OFS $3} !($4 in hdr){hn[++h]=$4; hdr[$4]} k in row{row[k]=row[k] OFS $5; next} {rn[++n]=k; row[k]=$5} END { printf "%s", rn[1] for(i=1; i<=h; i++) printf "%s", OFS hn[i] print "" for (i=2; i<=n; i++) print rn[i], row[rn[i]] }' ifile.txt
Expanding on my answer from your previous question: gawk ' BEGIN { SUBSEP = OFS = "," month["Jan"] = "01"; month["Feb"] = "02"; month["Mar"] = "03"; month["Apr"] = "04"; month["May"] = "05"; month["Jun"] = "06"; month["Jul"] = "07"; month["Aug"] = "08"; month["Sep"] = "09"; month["Oct"] = "10"; month["Nov"] = "11"; month["Dec"] = "12"; } function timestamp_to_numeric(s) { # 03hr31Jan2001 => 200101310300 return substr(s,10,4) month[substr(s,7,3)] substr(s,5,2) substr(s,1,2) "00" } NR==1 {next} {g = timestamp_to_numeric($4); groups[g]; value[$1,$2,$3][g] = $5} END { PROCINFO["sorted_in"] = "#ind_str_asc" printf "x,y,z"; for (g in groups) printf ",%s", g; printf "\n" for (a in value) { printf "%s", a for (g in groups) printf "%s%s", OFS, 0+value[a][g] printf "\n" } } ' ifile.txt x,y,z,201801010100,201801010200,201801010300 1,1,5,3,3.1,3.2 1,3.4,3,4.1,6.1,1.1 1,4.2,6,6.33,8.33,5.33 3.4,1,2,3.5,5.65,3.66 3.4,3.4,4,6.32,9.32,12.32 3.4,4.2,8.1,7.43,7.93,5.43 4.2,1,3.4,6.12,7.15,9.12 4.2,3.4,5.5,2.2,3.42,3.21 4.2,4.2,6.2,1.3,3.4,1 You have to create a mapping between the month name and the month number, then create a function to transform the timestamp to the new format. Beyond that, the code is the same.
How to properly position and activate a hyperlink in PDF?
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.
How to program a circle of radius r and center x,y with annotation box positioned bottom,left of x,y using PURE PDF 1.7 specification
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.
How to create pdf-file and save to the server?
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); ?>