2

I'm having a document whose source files are kept in git, and I'd like something similar to Print subversion repo number on each page ... I've already managed to pull the information I need and show it in the doc - except there's one problem: pretty much either svn or git require you to commit first, then re-build the PDF, so that you can access the latest revision number/ID (to insert it in the document PDF).

Now, I have a script, that automatically uploads the document PDF to a location, whenever a commit has been (successfully) made. If I want to include versioning info in there - I'd have to run pdflatex yet again after a commit has been made, and before upload has commenced; given that the document is huge, that would mean I'd have to wait extra half-a-minute to a minute each time I commit, which would irritate me immensely :)

So this is what I was thinking: if there was something like a PDF "string" variable, which could end up uncompressed in the output PDF during the TeXing step; then I could simply use sed -i or something to replace it's content (here the revision info) inline using minimal time, right after a commit is made.

Here is what I mean: if I look at a typical PDF produced by pdflatex with less -L, I usually get something like this:

%PDF-1.5
%<D0><D4><C5><D8>
3 0 obj
<<
/Length 463       
/Filter /FlateDecode
>>
stream
xڭ<95>OO<E3>@^L<C5><EF><F9>^T> ...

That is, the very first object definition is "plain text", but its contents are a compressed stream... (so in principle I could unpack those, and replace content there, but since I'm trying to make it as simple as possible, I'm not interested in that).

Now, the PDF spec doesn't really have a "string variable":

... but it does have an XObject, which you can define once, and then re-use each page with Do: the problem with it is that is absolute (but I'd live with just bottom left placement).

So this is the thing; if I could instruct Latex to insert the XObject definition, e.g.:

6 0 obj
  << /Type /XObject
     /Subtype /Form
     /FormType 1
     /BBox [ 0 0 1000 1000 ]
     /Matrix [ 1 0 0 1 0 0 ]
     /Resources << /ProcSet [ /PDF ] >>
     /Length 58
  >>
stream
  %70 50 TD     % without this `TD` setting, `/A Do` places this in 0,0 - bottom left corner
  /F1 12 Tf
  (Hello, world!) Tj
endstream
endobj

... at the start of the PDF document, in uncompressed format; and then if I use say fancyhdr to insert /A Do in every page; then once the PDF document is built, I could basically just do sed -i 's/(Hello, world!)/(rev. 117)/' out.pdf after the commit is made, and be done with it.

(Of course, this isn't all - we'd need to insert also, say:

/XObject  <<
          /A 6 0 R  % XObject /A is obj 6 0
>>

... in the /Page dictionary (not sure if it can reside in /Pages), but that doesn't really need to be uncompressed.)

So, in general - is this kind of PDF intervention possible with LaTeX engines (pdf, lua, xe)?

  • 1
    You may try pdftk (stamp or background operations). – Paul Gaborit Nov 24 '14 at 0:45
  • 1
    Or you may try the pdfpages package... – Paul Gaborit Nov 24 '14 at 0:46
  • 2
    Why not change the script so that it compiles and uploads the PDF and no need to compile it before committing? (You probably need 2 compilations for update - at least I do.) – cfr Nov 24 '14 at 1:56
  • Thanks all; @PaulGaborit - I know pdftk, but didn't think of it in this context, that might probably be the best process-wise (but it will kill the /Producer (pdfTeX-1.40.15) document tag which I otherwise like :))... pdfpages will still require recompilation as far as I know? – sdaau Nov 24 '14 at 10:06
  • @cfr - the build script already does, say pdflatex; biber; pdflatex; pdflatex, and my commit script already uploads the pdf without compilation; the thing is, I'd like the "latest" (or "current") revision number/ID printed in the doc, and that number/ID is not available until the commit is made (which implies another recompilation after the commit, to get the right number/ID inside)... Cheers! – sdaau Nov 24 '14 at 10:09
2

You can use

\pdfinfo{/myversion (0.00.00)}

to include the Name /myversion (you should use some other handle that's unique to you; ideally you would register a name prefix with Adobe/ISO) with the uncompressed string 0.00.00 in the info dict of the resulting pdf. You can then easily replace the 0.00.00 with your current version (e.g. with sed), but the replacement must have the same length.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.