Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PDF generation techniques in Python and Django

Denis Cornehl
March 17, 2015
57

PDF generation techniques in Python and Django

Denis Cornehl

March 17, 2015
Tweet

Transcript

  1. %PDF-1.5 %ÐÔÅØ 3 0 obj << /Length 85 /Filter /FlateDecode

    >> stream [datadatatata] endstream endobj What is PDF? 12 0 obj << /Type /XRef /Index [0 13] /Size 13 /W [1 2 1] /Root 10 0 R /Info 11 0 R /ID [<AB1DA15EE92724083AEE2267259BC796> <AB1DA15EE92724083AEE2267259BC796>] /Length 49 /Filter /FlateDecode >> stream [datadatatata] endstream endobj startxref 10626 %%EOF
  2. %PDF-1.5 %ÐÔÅØ 3 0 obj <</Trapped /False /PTEX.Fullbanner (This is

    pdfTeX, Version 3.14159265-2.6-1.40.15 \(TeX Live 2014\) kpathsea version 6.2.0) /Producer (pdfTeX-1.40.15) >> endobj 6 0 obj <</Length 7 0 R >>stream BT /F8 9.9626 Tf 134.144 480.611 Td [(testletter)]TJ 169.365 -367.259 Td [(1)]TJ ET endstream endobj What is PDF? 9 0 obj <</StemV 69 /XHeight 431 /Flags 4 /Ascent 694 /FontName /AGXOTB+CMR10 /Descent -194 /Type /FontDescriptor /ItalicAngle 0 /CharSet (/e/l/one/r/s/t) /FontBBox [ -40 -250 1009 750 ] /FontFile 11 0 R /CapHeight 683 >> endobj startxref 10626 %%EOF
  3. Write PDF with Django Templates? What you have to do

    yourself: • Line breaks • Graphs • Images / Positioning • Page breaks • Header / Footers
  4. Requirements • Simple • Nice PDF output • Ability to

    implement a design (logo, fonts) • Performance • Easy to integrate into Django/Python • complex content (in our case)
  5. Reportlab from reportlab.lib import colors
 from reportlab.lib.pagesizes import letter
 from

    reportlab.platypus import SimpleDocTemplate, Table, TableStyle
 
 doc = SimpleDocTemplate("simple_table.pdf", pagesize=letter)
 # container for the 'Flowable' objects
 elements = []
 
 data= [['00', '01', '02', '03', '04'],
 ['10', '11', '12', '13', '14'],
 ['20', '21', '22', '23', '24'],
 ['30', '31', '32', '33', '34']]
 t=Table(data)
 t.setStyle(TableStyle([('BACKGROUND',(1,1), (-2,-2),colors.green),
 ('TEXTCOLOR',(0,0),(1,-1),colors.red)]))
 elements.append(t)
 # write the document to disk
 doc.build(elements)
  6. xhtml2pdf <html> <head> <style> @page { size: a4 portrait; @frame

    header_frame { -pdf-frame-content: header_content; left: 50pt; width: 512pt; top: 50pt; height: 40pt; } @frame content_frame { left: 50pt; width: 512pt; top: 90pt; height: 632pt; } </style> </head> <body> <div id="header_content">Lyrics-R- Us</div> To PDF or not to PDF </body> </html>
  7. pdfkit / wkhtmltopdf • webkit rendering engine • basically “print

    to pdf” • with some command line options for page formatting
  8. LaTeX \documentclass{letter} \usepackage{hyperref} \signature{Joe Bloggs} \address{21 Bridge Street \\ Smallville

    \\ Dunwich DU3 4WE} \begin{document} \begin{letter}{Director \\ Doe \& Co \\ 35 Anthony Road \\ Newport \\ Ipswich IP3 5RT} \opening{Dear Sir or Madam:} I am writing to you on behalf of the Wikipedia project (http:// www.wikipedia.org/), an endeavour to build a fully-fledged multilingual encyclopaedia in an entirely open manner, to ask for permission to use your copyrighted material. \closing{Yours Faithfully,} \end{letter} \end{document}
  9. LaTeX Templates \documentclass{letter} \usepackage{hyperref} \signature{Joe Bloggs} \address{21 Bridge Street \\

    Smallville \\ Dunwich DU3 4WE} \begin{document} \begin{letter}{Director \\ Doe \& Co \\ 35 Anthony Road \\ Newport \\ Ipswich IP3 5RT} \opening{Dear <% last_name %> :} <!% if whatever %!> add this content <!% endif %!> \closing{Yours Faithfully,} \end{letter} \end{document}