Create PDF Report From Images in DIAdem Python Script

Updated Apr 30, 2024

Environment

Software

  • DIAdem

This article demonstrates how to use a Python script in DIAdem to generate a PDF report from images.

This approach may be necessary if exporting a DIAdem report as a PDF results in undesirable scaling issues. By first exporting the report sheets as images, external Python modules can be used to create a PDF document that concatenates the images, ensuring that each image fills an entire page.

Prerequisites

Follow the steps below to develop a Python script in DIAdem that exports report sheets as PNG files and generates a PDF report from the images.
 
  1. Ensure that the required Python modules are installed in DIAdem:
    1. In DIAdem, select the Settings >> DIAdem Settings... menu option.
    2. In the pop-up window, select the SCRIPT tab.
    3. In the Python module name input, enter the below module names and click Install/Update Module after each.
      • pywin32
      • fpdf
DIAdem Install Python Module.PNG
  1. In the DIAdem SCRIPT Panel, create a new Python script:
    1. Select File >> New Python from the menu bar.
    2. Save the Python script.
  2. Underneath line 22 (where # -- Beginning of user code -- appears), import the required libraries as shown below.
from fpdf import FPDF # Library used to generate a PDF from images
  1. Define variables that define where to save the images and PDF report. In the example code below:
    • savePDFReportPath defines the directory of the images and PDF document.
    • reportName defines the file name of the final PDF.
savePDFReportPath = "C:\\Users\\PVE\\Desktop\\PDF From Images\\"
reportName = "PDF From Images"
  1. [OPTIONAL] Load a template report into DIAdem.
    • If a template report is not being used, additional code is necessary to populate the DIAdem report sheet(s) with data.
dd.Report.LoadLayout(savePDFReportPath + "example_template.TDR")
  1. Export each report sheet as an image using the dd.Report.ActiveSheet.ExportToImage command.
    • In the code snippet below, a For Loop is used to export each report sheet as a PNG file, ensuring that the aspect ratio is maintained.
    • After each export, the imagelist variable is updated to store the paths of each PNG.
imagelist = []

for i in range(1, dd.Report.Sheets.Count + 1):
    dd.Report.Settings.ImageExport.PNG.BitsPerPixel = 4
    dd.Report.Settings.ImageExport.PNG.UseRatio = True
    dd.Report.Settings.ImageExport.PNG.Height = 768
    
    dd.Report.Sheets.Item(i).Activate
    dd.Report.ActiveSheet.ExportToImage(savePDFReportPath + "Image" + str(i) + ".PNG", dd.eImageExportTypePNG)
    imagelist.append(SavePDFReportPath + "Image" + str(i) + ".PNG")
  1. Using the fpdf library, create a PDF document that places each exported image on a page (where each image fills the entire page).
    • It is crucial that the arguments in pdf.image() are set so that the image spans the entire width and height of the page. To identify the height and width in your DIAdem report:
      1. Select the REPORT Panel in DIAdem.
      2. From the menu bar, select Settings >> Layout Setup >> Layout Parameters...
      3. In the Page size option, select User-defined and Scaled
      4. Configure the Height and Width in inches.
      5. In the pdf.image() function, configure the height and width arguments in millimeters.
pdf = FPDF(unit="mm")

for image in imagelist:
    pdf.add_page()
    
    '''pdf.image() palces an image on the PDF page and sizes it according to the arguments. Below, it is assumed that the PDF is A4 (e.g. width = 210mm, height = 297mm).
    image(image,x,y,w,h) where:
    x = abiscissa from the top left corner
    y = ordinate from the top left corner
    w = width of the image in the page
    h = height of the image in the page'''
    pdf.image(image, 0, 0, 210, 297)

pdf.output(savePDFReportPath + reportName + ".pdf", "F")
  1. [OPTIONAL] Delete the now unrequired image files using the dd.FileDelete command.
for image in imagelist:
    dd.FileDelete(image, True)

After completing the steps above, a Python script similar to the attached .py file should exist. When the Python script executes, a DIAdem report (of n sheets) is exported into n images. The Python fpdf library concatenates the images into a single PDF document, and then the image files are deleted.