Adding PrinceXML as another PDF Engine

Created on 17 September 2022, over 2 years ago
Updated 4 September 2023, over 1 year ago

Problem/Motivation

We supported several Drupal 7 sites which used the PrinceXML commercial PDF engine. Unfortunately a PrinceXML interface was not supported past Drupal 7. We tried to use the existing PDF engines for Drupal 8/9, but could not get our output to match what we did with Prince. We ended-up writing our own interface.

The downside of this is we have to patch Printable with every release to work; Ideally PrinceXML would be supported natively.

Proposed resolution

I'm attaching documentation and patches on what I've changed to work with the latest Printable 2.1.4 release. So even if we cannot get PrinceXML added, at least others might have a way to use it.

Big picture steps:

  1. I took the methodology used for the wkhtmltoPDF and created princexmltoPDF. Forking the vendor "mikehaertl" to my own local area, creating "phpprincexmltopdf" in place of "phpwkhtmltopdf", and patching "php-shellcommand/src/Command.php" to use prince engine.
  2. The pdf_api module gets a new file added for prince, and its pdf_api.services.yml changed to use it.
  3. The main composer.json gets an autoload section to make use of the phpprincexmltopdf classes.
  4. printable gets a couple of changes to make use of the new princexmltoPDF option.

How to install:

  1. You should already have printable installed, and setup to use the wkhtmltoPDF option. If that works then you must have the mikehaertl vendor code installed along with the contrib pdf_api module. For reference, these patches were made with Drupal 9.3.21, pdf_api 2.2.2, printable 2.1.4
  2. If you have gotten this far then I assume you already have PrinceXML. If you don't, it is free for non-commercial use (though it does print its own logo on the generated PDFs). You can download it at https://www.princexml.com/latest/
  3. All commands are assumed run from your Drupal web root folder, and that you have put the patch files in that same folder.
  4. unzip 2495551_princexml_patches.zip and put the patch files in your Drupal web root folder.
  5. mkdir -p modules/custom/princexml/vendor
  6. cp -Rp ../vendor/mikehaertl/* modules/custom/princexml/vendor/
  7. patch -p1 < 2495551_vendor_princexml.patch
  8. patch -p1 < 2495551_pdf_api_222_princexml.patch
  9. patch -p1 < 2495551_printable_214_princexml.patch
  10. Edit your main composer.json (vi ../composer.json), adding a new (or editing your existing) autoload section to look like
        },
        "autoload": {
            "psr-4": {
                "ljackwilson\\princexmlto\\": "web/modules/custom/princexml/vendor/phpprincexmltopdf/src",
                "ljackwilson\\tmp\\": "web/modules/custom/princexml/vendor/php-tmpfile/src",
                "ljackwilson\\shellcommand\\": "web/modules/custom/princexml/vendor/php-shellcommand/src"
            }
        }
    }
    
  11. Refresh your cache from the site (or if using drush then drush cr)
  12. Now go to the printable configuration page at /admin/config/user-interface/printable/pdf. You should have a new radio button option for princexmltoPDF under the PDF Generation Tool section.
  13. At the bottom of this same page should be a "Full Path to Prince binary file" field. Mine is /usr/bin/prince
  14. Cleanup your web folder, removing the .zip and .patch files.
  15. Submit your printable config changes, and test!
  16. Currently debugging to the drupal log is enabled, and the temporary pdf and html files that Prince creates are not saved. If you want them saved during testing then edit modules/custom/princexml/vendor/phpprincexmltopdf/src/Pdf.php. Where to make those changes is commented.

Remaining tasks

Since this makes changes to printable, pdf_api, and mikehaertl, I'm not sure what the process would be to make that happen in an official way. And if no one else needs PrinceXML, then it isn't worth the effort. But I assume since it was available for Drupal 7 then maybe there is sufficient interest to make it work for Drupal 9 and beyond.

✨ Feature request
Status

Active

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States ljwilson

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¦πŸ‡ΊAustralia nigelcunningham Geelong

    Hi again.

    I've taken a look today at adding this, and would like to do some tidyups before merging it - will it be a problem to you if I commit a modified version?

Production build 0.71.5 2024