Skip to content

Instantly share code, notes, and snippets.

@devharsh
Last active December 12, 2021 22:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
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