Comment by bad_username

2 days ago

Not the same thing, but I found a way to distribute markdown sources (with images) within the PDF files generated from these sources.

The trick is to generate the PDF normally, then zip this same PDF together with the sources again, with compression level 0, making sure that the PDF is the first file to go in the archive. (Easy to write a script that does this.)

The resulting file, when given the extension PDF, is readable as PDF, and when given the extension ZIP, is extractable as ZIP. So whoever wants the source can rename the file to .zip and extract the source. The instruction to do so can be in the PDF text itself.

Why it works: a) compression level 0 means that the input files are just copied into the stream, so the PDF reader will find the PDF header, decode the rest of the PDF, and ignore the trailing stuff. The trailing stuff contains the markdown sources and the zip directory, making the file a valid archive.

I suspect that tolerances in PDF readers and ZIP decompressors are being slightly abused here, but it works with all PDF readers and ZIP decompressors that I tried so far.

That seems like it would be incredibly fragile. As soon as the receiving party made a change that required re-saving the PDF -- like commenting, highlighting, changing default layouts, saving as a PDF/a, checking PDF/ua, etc. -- it might erase the attached files.

It's also very easy to use pdftk to embed or attach files in a PDF using the methods defined in the PDF standard. No renaming or special knowledge required of the audience.

Attachments are a feature of PDF; I often attach LaTeX sources to the PDF output.