Skip to content

Instantly share code, notes, and snippets.

@ASolchen
Created April 23, 2024 16:12
Show Gist options
  • Save ASolchen/41ae049823fc7d4bb74ab55502137f99 to your computer and use it in GitHub Desktop.
Save ASolchen/41ae049823fc7d4bb74ab55502137f99 to your computer and use it in GitHub Desktop.
Print PDF Report
from subprocess import Popen, PIPE
import os, time, datetime
cwd = os.getcwd()
ACROBAT = "C:\Program Files\Adobe\Acrobat DC\Acrobat\Acrobat.exe"
PDF = os.path.join(cwd, 'sample.pdf')
TEMP_FILE = os.path.join(cwd, 'Temp.html')
from pyhtml2pdf import converter
HTML = """
<!DOCTYPE html>
<html lang="en">
<style>
table, th, td {
border:1px solid black;
font-size: 36px;
}
table{width: 80%;
text-align: center;
margin-left: auto;
margin-right: auto;
margin-top: 100px;
}
footer {
font-size: 24px;
background-color: #CCC;
width: 100%;
text-align: center;
color: black;
position: absolute;
bottom: 0;
}
</style>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Truck Unload Report</title>
</head>
<body>
<table>
<tr>
<th>Item</th>
<th>Value</th>
</tr>
<tr>
<td>Truck ID</td>
<td>1111</td>
</tr>
<tr>
<td>Date</td>
<td>4-JUL-2024</td>
</tr>
<tr>
<td>Product</td>
<td>Super Stuff</td>
</tr>
<tr>
<td>Amount (Gal)</td>
<td>7520</td>
</tr>
</table>
<footer>
<p>Print Date 04-Apr-2024</p>
</footer>
</body>
</html>
"""
# Read PLC and get values
"""
with LogixDiver(PLC_ADDRESS) as plc:
tags = plc.read(['Tag01', 'Tag02'])
"""
#replace the html text with the values
"""
for tag in tags:
HTML = HTML.replace(tag.tagname, str(tag.value))
"""
#write the temp file
with open(TEMP_FILE, "w") as fp:
fp.write(HTML)
#convert the html to a pdf
converter.convert(f'file:///{TEMP_FILE}', PDF)
#call acrobat to print
p1 = Popen([ACROBAT, '/t', PDF], stdout=PIPE) #run acrobat with /t to open and print to the default printer
time.sleep(1) # wait a second for acrobat to print
p1.stdout.close() # close acrobat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment