Example #1
0
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.request, urllib.parse, urllib.error

    pdf = pisa.CreatePDF(
        urllib.request.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)
Example #2
0
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.
    try:
        # First try newer version of library under a different name.
        import xhtml2pdf.pisa as pisa
        pisa  # pyflakes
    except ImportError:
        # Try the old library.  Note that you can also get the above
        # ImportError simply because you are on Python 2.4 and
        # xhtml2pdf.pisa tries and fails to import hashlib.
        import ho.pisa as pisa

    file_path = '%s/%s' % (export_dir, filename)

    pdf_file = file(file_path, "wb")
    link_callback = pisa.pisaLinkLoader(original_url).getFileName

    pdf = pisa.CreatePDF(source,
                         pdf_file,
                         log_warn=1,
                         log_err=1,
                         path=original_url,
                         link_callback=link_callback,
                         )

    if pdf.err:
        return None, pdf.err

    return pdf_file, None
Example #3
0
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.
    """
    pdf = pisa.CreatePDF(urlopen(url),
                         open(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)