def testURL( url="http://www.htmltopdf.org", dest="test-website.pdf"): """ Loading from an URL. We open a file like object for the URL by using 'urllib'. If there have to be loaded more data from the web, the pisaLinkLoader helper is passed as 'link_callback'. The pisaLinkLoader creates temporary files for everything it loads, because the Reportlab Toolkit needs real filenames for images and stuff. Then we also pass the url as 'path' for relative path calculations. """ import urllib pdf = pisa.CreatePDF( urllib.urlopen(url), file(dest, "wb"), log_warn = 1, log_err = 1, path = url, link_callback = pisa.pisaLinkLoader(url).getFileName ) dumpErrors(pdf) if not pdf.err: pisa.startViewer(dest)
def createFromHtml(self, url, outputSize = 'b6', color=0, style=None, landscape=0, number=0): cssInsert = DEFAULT_CSS + """ @page { @frame { size: %s } size: %s; } """ % (outputSize, outputSize) logging.info("cssInsert: " + cssInsert) pdfBuffer = cStringIO.StringIO() page = urllib.urlopen(url) logging.info(page) pdf = pisa.CreatePDF( page, pdfBuffer, format = outputSize, default_css = cssInsert, log_warn = 1, log_err = 1, path = url, link_callback = pisa.pisaLinkLoader(url).getFileName ) return pdfBuffer.getvalue()
def html_to_pdf(source, export_dir, filename, original_url, use_print_css, extra_options=[]): # We import pisa inside the function so it does not raise # import exception if pisa is not installed. import ho.pisa as pisa file_path = '%s/%s' % (export_dir, filename) pdf_file = file(file_path, "wb") pdf = pisa.CreatePDF( source, pdf_file, log_warn = 1, log_err = 1, path = original_url, link_callback = pisa.pisaLinkLoader(original_url).getFileName, ) if pdf.err: return None, pdf.err return pdf_file, None