Server

HOWTO: Automatically Convert a Document to PDF

Updated: December 5, 2024

In some cases, you want the system to automatically create a PDF conversion of a document and attach it to the document. In Nuxeo Studio, the operation Conversion > Convert To PDF is provided to make this conversion.

Here is how you can create an automation chain to do the conversion and attach the PDF created on the input document.

This how-to requires knowledge about:

Document Preparation

The document needs a metadata to hold the PDF blob created during the conversion, two options are available:

  • Use the default File document type, in that case, the metadata that holds the blob is file:content.
  • Define your own custom metadata to store the blob. To do so, you first need to add a field to the schema in the document definition. This new metadata has to be a Blob type. Here is an example where the new metadata is pdffile. It can be accessed by myDocumentSchema:pdffile if the schema name of my document type is myDocumentSchema.

Automation Chain

The Blob To PDF operation accepts either blob, bloblists or document as inputs. If the input is a document, the file to convert must be in the file:content metadata (the usual place for it). The operation produces a blob (the PDF file) and does not return the input document so we will have to store the document somehow before the conversion so that we can recall it after to attach the PDF file to it. The solution is to use Push & Pop operation to put the input document in a heap and get it back (or to use a context variable).

For the same reasons, we also do not want to lose the PDF file produced by the conversion during the recall of the input document, so we will save it as a Context variable.

The automation chain to configure is finally:

- Context.FetchDocument
- Context.PushDocument
- Blob.ToPDF
- Context.SetInputAsVar:
    name: pdfblob
- Context.PopDocument
- Document.SetBlob:
    file: "@{Context[\"pdfblob\"}"
    save: "true"
    xpath: "myDocumentSchema:pdffile"