Last active
December 12, 2021 22:50
-
-
Save devharsh/efc8b40c94bcbab7e42d4d346861bbb6 to your computer and use it in GitHub Desktop.
Create PDF from scratch - write strings to PDF in Python. For reference and credits please visit: http://com.puter.tips/2016/12/create-pdf-from-python.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
from reportlab.platypus import SimpleDocTemplate, Paragraph | |
from reportlab.lib.styles import getSampleStyleSheet | |
from reportlab.lib.units import inch | |
from reportlab.lib.pagesizes import letter | |
buf = io.BytesIO() | |
doc = SimpleDocTemplate( | |
buf, | |
rightMargin=inch/2, | |
leftMargin=inch/2, | |
topMargin=inch/2, | |
bottomMargin=inch/2, | |
pagesize=letter, | |
) | |
styles = getSampleStyleSheet() | |
paragraphs = [] | |
paragraphs.append(Paragraph('This is first line', styles['Normal'])) | |
paragraphs.append(Paragraph('This is last line', styles['Normal'])) | |
doc.build(paragraphs) | |
with open('demo.pdf', 'wb') as fd: | |
fd.write(buf.getvalue()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment