DEV Community

Cover image for Hack WKHTMLTOPDF PDF to enable Adobe Acrobat Field Editing
James Moberg
James Moberg

Posted on

Hack WKHTMLTOPDF PDF to enable Adobe Acrobat Field Editing

I recently integrated the auto-generation of survey results into a downloadable PDF using ColdFusion and WKHTMLTOPDF 0.12.6. Our client provided a pre-generated PDF cover page with some editable fields that we prepended to the PDF using PDFtk. Unfortunately, all auto-generated bookmarks became unusable after cover page is prepended.

I generated the HTML for a new cover page by using PDF-XChange Editor Plus (portable, perpetual license) to determine the font size, font face and element spacing. In the end, this new made-with-WKTHMLTOPDF version looked far superior to the client-provided cover and seemed to work fine... until my business partner reviewed it. He indicated that it only worked in Edge, but not in Google Chrome or Adobe Acrobat. This seemed very odd since I initially tested it using PDF-XChange. I then tested it using Firefox, Foxit PDF Reader and even the Total Commander List plugin. The PDF's input fields are 100% editable, printable & able to be saved. I then tried it using a workstation that has Adobe Acrobat Reader installed and, sure enough, none of the input fields were editable. $%@!

This issue was a difficult to research, but I did find a complaint on GitHub. The problem is due to the fact that an "interactive" flag is missing, but I don't believe that this is even a feature within WKHTMLTOPDF (apart from the --enable-forms option). WK also generates a 1.4 versioned PDF. Is the interactive option even available in v1.4 PDFs? The hack to make PDFs compatible with Adobe Acrobat required modifying the binary file and performing a search-and-replace. (You can use NotePad++ to do this manually.) Apparently, the Parent string in Annot \Parent nn needs to be changed to something other than Parent. (I chose to use Papent since it was the same length.) The regex pattern Annot\s.{1,5}Parent\s+\d+ enabled the identification of the Parent nn string that needed to be changed. This approach required performing a simple read of the file (to identify the string pattern to use) and then reading the binary PDF file, converting the binary data to HEX and then performing the search-and-replace using HEX-encoded strings.

SHORT STORY: This workaround of having to modify binary files manually transported me back to 1980's when I was a founding member of HACK (Hibbing Area Commodore Club). My parents purchased a C64 for Christmas, but refused to purchase any games. I was a teen and many of my early programming days required entering, searching and/or editing HEX code. I personally purchased books & magazines to "learn-by-doing" and sought out others in the area with similar interests. In 1990, I served as a Chaplain Assistant in the Army in Germany and used GeoWorks for desktop publishing all chapel-related printed publications so that our promoted events looked more appealing on the bulletin boards than the night club located next door. After that, I learned Lotus Approach at my job while attending college. I merged database entries to create a really nice resource book for my senior capstone project. I later used Microsoft Access to convert my capstone project into my first database-driven website using Allaire Cold Fusion 3.0 back in 1997. I've continued being a ColdFusion/CFML developer as I'm still able to get it to do most of what I ever need it to do and enjoy the occasional challenge.

Generating PDFs via WKHTMLTOPDF

If you want to check out any CFML libraries to assist in generating PDFs using WKHTMLTOPDF, here's a couple options:

Source Code

https://gist.github.com/JamoCA/607e2b1b28f2a55006ba9bdf26d4df9b

Top comments (0)