Index ¦ Archives ¦ RSS

Improved PDF export options in the command-line and in Online

Estimated read time: 3 minutes

The LibreOffice Technology now has much better support for creating custom PDF exports of documents: options available in the interactive PDF export options dialog are now also possible to set from the command-line, and also when using the document conversion feature of Collabora Online.

Motivation

I was working on a regression that only happens if you export the second and third pages of a document to PDF. While investigating, I needed a quick way to trigger the problematic code-path, and clicking through a dialog is not convenient.

https://share.vmiklos.hu/blog/pdf-convert-to/pdf-export-partial.png
Figure 1. UI to specify a partial export to PDF

The PDF export in LibreOffice has a lot of options, but those are only available from extension code (using the UNO API) or from the UI dialog. The idea was to expose much of this to the soffice --convert-to command as well, and if we’re at it, also to the /cool/convert-to endpoint of Online.

Results so far

Here are a few examples to show the possibilities:

  • Skip the first page of a Draw document:

soffice --convert-to 'pdf:draw_pdf_Export:{"PageRange":{"type":"string","value":"2-"}}' test.odg
  • Add watermark:

soffice --convert-to 'pdf:draw_pdf_Export:{"TiledWatermark":{"type":"string","value":"draft"}}' test.odg
  • Encrypt the output:

soffice --convert-to 'pdf:draw_pdf_Export:{"EncryptFile":{"type":"boolean","value":"true"},"DocumentOpenPassword":{"type":"string","value":"secret"}}' test.odg
  • We default to PDF 1.6, perhaps you want to write PDF 1.5:

soffice --convert-to 'pdf:draw_pdf_Export:{"SelectPdfVersion":{"type":"long","value":"15"}}' test.odg
  • Sign the PDF:

soffice --convert-to 'pdf:draw_pdf_Export:{"SignPDF":{"type":"boolean","value":"true"},"SignCertificateSubjectName":{"type":"string","value":"CN=..."}}' test.odg

And the best is that the same also works in Online, e.g.:

curl -k -F "data=@3page.odg" -F "format=pdf" -F "options={\"PageRange\":{\"type\":\"string\",\"value\":\"2-\"}}" https://localhost:9980/cool/convert-to > out.pdf

How is this implemented?

If you would like to know a bit more about how this works, continue reading… :-)

Want to start using this?

You can get a snapshot / demo of Collabora Office and try it out yourself right now: try unstable snapshot. Collabora intends to continue supporting and contributing to LibreOffice, the code is merged so we expect all of this work will be available in TDF’s next release too (7.4).

For the Online side, see the CODE page, the feature is present in the current release (2021).

© Miklos Vajna. Built using Pelican. Theme by Giulio Fidente on github.