상세 컨텐츠

본문 제목

Django Pisa Html To Pdf

카테고리 없음

by redtetazuc1974 2020. 3. 2. 22:17

본문

  1. Django Pisa Html To Pdf Converter
  2. Xhtml2pdf Django

The client wanted to give their users the option of printing completed forms to a pdf file. They also wanted the pdf to be rendered with formatting and style that varied slightly from the online display of the completed form, and so desired a solution other than the browser’s own print function. The open-source Reportlab library is a popular solution for generating on-the-fly pdfs, and the xhtml2pdf library, which depends on Reportlab, offers a relatively easy way to convert an html web page to pdf while (more-or-less) preserving css styles. For these reasons, we chose an approach using xhtml2pdf and ReportLab for our client’s request.I added “xhtml2pdf0.0.6” to “requirements.txt” to mark it as a dependency of the project.Installing this dependency into the project (e.g., by running “pip install -r requirements.txt”) will pull in further dependencies. This allows the following lines to be added to 'views.py': import cStringIO as StringIOfrom xhtml2pdf import pisafrom django.template.loader import gettemplatefrom django.http import HttpResponseWe used class-based views to render each of the forms we wanted to reproduce as a pdf.

Django Pisa Html To Pdf Converter

PdfHtml to pdf online free

Xhtml2pdf Django

Def renderpdfweasyprint( html):from weasyprint import HTMLpdf = HTML( string =html.encode( 'utf-8 '))return pdf.writepdfdef renderpdfxhtml2pdf( html):'mimerender helper to render a PDF from HTML using xhtml2pdf.Usage: xhtml2pdf import pisafrom cStringIO import StringIOpdf = StringIOpisa.CreatePDF(StringIO(html.encode( 'utf-8 ')), pdf)resp = pdf.getvaluepdf.closereturn respdef renderpdfphantomjs( html):'mimerender helper to render a PDF from HTML using phantomjs. '# The 'makepdf.js' PhantomJS program takes HTML via stdin and returns PDF binary via stdout# Another approach would be to have PhantomJS do a localhost read of the URL, rather than passing html around.from subprocess import Popen, PIPE, STDOUTimport osp = Popen( 'phantomjs ', '%s/pdf.js '% os.path.dirname(os.path.realpath( file)), stdout = PIPE, stdin = PIPE, stderr = STDOUT)return p.communicate( input =html.encode( 'utf-8 ')) 0.