-
Notifications
You must be signed in to change notification settings - Fork 16
/
pdf.py
30 lines (25 loc) · 948 Bytes
/
pdf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from xhtml2pdf import pisa # import python module
from StringIO import StringIO
# Define your data
sourceHtml = ""
outputFilename = "test.pdf"
def create_pdf(pdf_data):
pdf = StringIO()
pisa.CreatePDF(StringIO(pdf_data.encode('utf-8')), pdf)
return pdf
# Utility function
def convertHtmlToPdf(sourceHtml, outputFilename="test.pdf"):
# open output file for writing (truncated binary)
resultFile = open(outputFilename, "w+b")
# convert HTML to PDF
pisaStatus = pisa.CreatePDF(
StringIO(sourceHtml.encode('utf-8')), # the HTML to convert
dest=resultFile) # file handle to recieve result
# close output file
resultFile.close() # close output file
# return True on success and False on errors
return pisaStatus.err
# Main program
if __name__=="__main__":
pisa.showLogging()
convertHtmlToPdf(sourceHtml, outputFilename)