Store a Python generated PDF in Docker

Where a Python script has to write a PDF in a container so the file survives a container update and can be shown by a Document Viewer.

Updated 2 min read
On this page (3 sections)
  1. Where the file has to be written
  2. Write the PDF from a Python script
  3. Show the file in a Document Viewer

Where the file has to be written

A container keeps nothing of its own file system. Everything written outside the EnvisionVolume volume belongs to the container and is gone the moment the container is replaced, which is what happens on every version update. A PDF produced by a Python script therefore has to be written to a folder on the volume.

The volume is mounted at /EnvisionVolume and the project folder inside it is /EnvisionVolume/Project. The /EnvisionVolume/Project/Reports subfolder is the folder the Report Designer writes to when Output Path is empty, which makes it the natural destination for a document a script produces as well. Any other folder on the volume works too, and the script can create it.

Path used in the scriptResult
/EnvisionVolume/Project/Reports/TankLevel.pdfStored on the volume. Kept when the container is replaced.
/EnvisionVolume/Documents/TankLevel.pdfStored on the volume. Any folder of your own, created by the script.
TankLevel.pdf, without a folderStored inside the container next to the application. Lost when the container is replaced.
/app/Project/TankLevel.pdfStored inside the container. Lost when the container is replaced.

On a Windows installation the same script writes to the Project\Reports folder of the installation folder instead. The /EnvisionVolume mount point exists in the container only.

Write the PDF from a Python script

The example writes a one page PDF with fpdf, which ships with the product and needs no installation. Only the folder matters here; the content of the document is free.

Write a PDF to the volume
import os
from fpdf import FPDF

folder = "/EnvisionVolume/Project/Reports"
os.makedirs(folder, exist_ok=True)

pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=14)
pdf.cell(0, 10, "Tank 1 level: %s" % GetFloatValue("Tank1_Level"))
pdf.output(os.path.join(folder, "TankLevel.pdf"))

Writing the same file name on every run keeps one current document, which is what a Document Viewer with a fixed Document Path shows. Put the date in the file name when every run has to be kept.

Show the file in a Document Viewer

A Document Viewer serves a file only when the folder holding it is listed in the Document viewer access parameter, so the folder the script writes to has to be added there once.

  • Open Parameters, section Engineering Settings, and add the folder to Document viewer access, for example /EnvisionVolume/Project/Reports, together with the user groups that may see it. Leave the user groups empty to allow every logged in user group.
  • Place a Document Viewer on the page and set Document Path to the full path of the file, for example /EnvisionVolume/Project/Reports/TankLevel.pdf.
  • Set Document Path to the name of a client variable instead when the operator picks the document on the page.

The rules of the folder and user group list, the supported file types, and the other settings of the object are described in the Document Viewer article of the Design chapter. The parameter itself is described in the Parameters article, and running the product in a container is described in the Docker article.

Something missing or wrong in this article? Contact us