Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Industrial Technology

Generate PDF Reports from PLCnext Using Python and REST API

I was asked quite frequently how a PDF report with process data from the PLC program can be created with PLCnext. An easy way is to use a Python script which collects process data via the REST API and creates the PDF on the device. The creation of the script is triggered by a function block in IEC61131 on the PLC.

Here are the necessary steps:

  1. Install pip on the PLC for the user "plcnext_firmware". This is the user which is used by the function block that executes the Python.

     sudo passwd plcnext_firmware --> assign a new password
     su plcnext_firmware
     curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
     python3 get-pip.py
    
  2. Install the required Python libraries:

python3 -m pip install requests

python3 -m pip install fpdf

  1. Create a Python script and copy it to the /opt/plcnext/ folder on the PLC:
 import requests
 import json
 import sys
 from fpdf import FPDF
 from datetime import datetime
 sys.path.append('/opt/plcnext/.local/python3.10/site-packages/')
 class PDF(FPDF):
 def header(self):
 self.image('Logo.png', 10, 8, 40) # optional
 self.set_font('Arial', 'B', 20)
 self.cell(0,10,'Monthly report', border=False, ln=True, align='C')
 self.ln(20)
 def footer(self):
 self.set_y(-15) 
 self.set_font('Arial', 'I', 12)
 self.cell(0,10,f'Page {self.page_no()}/{{nb}}',align='C')
 ##################################
 Read data from the PLC via REST
 ##################################
 response = requests.get('https://192.168.178.10/_pxc_api/api/variables?paths=Arp.Plc.Eclr/INST_SolarEnergy.diPower', verify=False)
 data = response.json()
 variables = data['variables']
 value = variables[0]['value']
 ###############
 Timestamp 
 ###############
 now = datetime.now()
 current_time = now.strftime("%m.%d.%Y %H:%M:%S")
 ################
 Create PDF 
 ################
 pdf = PDF('P','mm','A4')
 pdf.alias_nb_pages()
 pdf.set_auto_page_break(auto=True, margin = 15)
 pdf.add_page()
 pdf.set_font('Arial', '', 12)
 mes = 'The power of the pump is ' + str(value) + ' W'
 pdf.cell(200, 10, 'Report '+current_time, ln=True) 
 pdf.cell(200, 10, txt = mes, ln=True) 
 pdf.output('Report.pdf')
  1. Create a new PLC project.

  2. In the project create a variable which can be accessed via the REST API. For local variables, the "HMI" tag must be selected.

  1. Import the PLCnextBase library into the project. The library is available in the PLCnext Store.

  2. Create a program which uses the PBCL_LinuxShell command to execute the Python script. Be aware that the PLC will perform a warmstart after the first execution of the FB.

  1. Download and start the program. The script is executed via the xExecuteScript variable. The pdf report is stored in the /opt/plcnext/ folder. The local variable xRetain must have the retain tag checked.

Note:

The Makers Blog shows applications and user stories of community members that are not tested or reviewed by Phoenix Contact. Use them at your own risk.


Industrial Technology

  1. Cams & Followers Explained: Types, Nomenclature, Function & Applications – Diagrams & PDF
  2. High-Performance Thermal Relief Valve Systems from Swagelok for Charbonneau Industries
  3. China Builds 13‑Times Hotter ‘Artificial Sun’ – A Leap Toward Clean Fusion Energy
  4. 24V Solar System Wiring: Parallel Panels & Series Batteries Guide
  5. Custom Structural Steel Fabrication for Facility Expansion & System Retrofitting – Trusted Expertise Since 1956
  6. Battery Separators Explained: How They Keep Your Devices Safe and Efficient
  7. Top 50 AIDC Solutions: Best Automatic Identification & Data Capture Tools
  8. Industrial Automation Explained: Types & Benefits for Smart Factories
  9. Harnessing Digital Technology to Drive Manufacturing Excellence
  10. Infographic: Essential Manufacturing Insights Revealed