Exemple #1
0
def generate_pdf(html='', url=''):
    # Validate input
    if not html and not url:
        raise ValueError('Must pass HTML or specify a URL')
    if html and url:
        raise ValueError('Must pass HTML or specify a URL, not both')

    wkhtmltopdf_default = (
            os_path.abspath(os_path.split(__file__)[0]) +
            '/bin/wkhtmltopdf-heroku')

    # Make sure wkhtmltopdf-heroku is executable
    chmod(wkhtmltopdf_default, 0755)

    # Reference command
    wkhtmltopdf_cmd = environ.get('WKHTMLTOPDF_CMD', wkhtmltopdf_default)

    # Set up return file
    pdf_file = NamedTemporaryFile(suffix='.pdf')

    if html:
        # Save the HTML to a temp file
        html_file = NamedTemporaryFile(suffix='.html')
        html_file.write(html)

        # wkhtmltopdf
        call_subprocess([wkhtmltopdf_cmd, '-q', html_file.name, pdf_file.name])

        # Clean up
        html_file.close()
    else:
        # wkhtmltopdf, using URL
        call_subprocess([wkhtmltopdf_cmd, '-q', url, pdf_file.name])

    return pdf_file
Exemple #2
0
def generate_pdf(html='', url=''):
    # Validate input
    if not html and not url:
        raise ValueError('Must pass HTML or specify a URL')
    if html and url:
        raise ValueError('Must pass HTML or specify a URL, not both')

    wkhtmltopdf_default = 'wkhtmltopdf-heroku'

    # Reference command
    wkhtmltopdf_cmd = environ.get('WKHTMLTOPDF_CMD', wkhtmltopdf_default)

    # Set up return file
    pdf_file = NamedTemporaryFile(delete=False, suffix='.pdf')

    if html:
        # Save the HTML to a temp file
        html_file = NamedTemporaryFile(delete=False, suffix='.html')
        html_file.write(html)
        html_file.close()

        # wkhtmltopdf
        call_subprocess([wkhtmltopdf_cmd, '-q', html_file.name, pdf_file.name])
    else:
        # wkhtmltopdf, using URL
        call_subprocess([wkhtmltopdf_cmd, '-q', url, pdf_file.name])

    return pdf_file
Exemple #3
0
def generate_pdf(html='', url=''):
    # Validate input
    if not html and not url:
        raise ValueError('Must pass HTML or specify a URL')
    if html and url:
        raise ValueError('Must pass HTML or specify a URL, not both')

    wkhtmltopdf_default = 'wkhtmltopdf'

    # Reference command
    wkhtmltopdf_cmd = environ.get('WKHTMLTOPDF_CMD', wkhtmltopdf_default)

    # Set up return file
    pdf_file = NamedTemporaryFile(delete=False, suffix='.pdf')

    if html:
        # Save the HTML to a temp file
        html_file = NamedTemporaryFile(delete=False, suffix='.html')
        html_file.write(html)
        html_file.close()

        # wkhtmltopdf
        call_subprocess([wkhtmltopdf_cmd, '-q', html_file.name, pdf_file.name])
    else:
        # wkhtmltopdf, using URL
        call_subprocess([wkhtmltopdf_cmd, '-q', url, pdf_file.name])

    return pdf_file
def generate_pdf(html='', url=''):
    # Validate input
    if not html and not url:
        raise ValueError('Must pass HTML or specify a URL')
    if html and url:
        raise ValueError('Must pass HTML or specify a URL, not both')

    wkhtmltopdf_default = '/app/.heroku/python/bin/wkhtmltopdf-heroku'

    # Reference command
    wkhtmltopdf_cmd = environ.get('WKHTMLTOPDF_CMD', wkhtmltopdf_default)

    if url:
        with NamedTemporaryFile(suffix='.pdf', mode='rwb+') as pdf_file:
            call_subprocess([wkhtmltopdf_cmd, '-q', url, pdf_file.name])
            pdf_file.seek(0)
            return pdf_file.read()

        # Save the HTML to a temp file
    with NamedTemporaryFile(suffix='.html', mode='w') as html_file:
        html_file.write(html.encode('utf-8'))
        html_file.flush()
        html_file.seek(0)
        with NamedTemporaryFile(suffix='.pdf', mode='rwb+') as pdf_file:
            # wkhtmltopdf
            call_subprocess(
                [wkhtmltopdf_cmd, '-q', html_file.name, pdf_file.name],
            )
            pdf_file.seek(0)
            return pdf_file.read()
    def generate_pdf(self, html, options=[]):
        wkhtmltopdf_default = 'wkhtmltopdf-heroku'
        # Reference command
        wkhtmltopdf_cmd = environ.get('WKHTMLTOPDF_CMD', wkhtmltopdf_default)
        # Set up return file
        pdf_file = NamedTemporaryFile(delete=False, suffix='.pdf')

        html_file = NamedTemporaryFile(delete=False, suffix='.html')
        html_file.write(html)
        html_file.close()

        # wkhtmltopdf
        call_subprocess([wkhtmltopdf_cmd, '-q'] + options + [
            html_file.name,
            pdf_file.name,
        ])

        return pdf_file
Exemple #6
0
def generate_pdf(syllabus):
    context_dict = syllabus.json_data
    context_dict['college'] = College.objects.get(pk=context_dict['college'])
    context_dict['department'] = Department.objects.get(
        pk=context_dict['department'])
    context_dict['rubric'] = Rubric.objects.get(
        pk=context_dict['rubric']).json_data

    template = get_template('syllabus/pdf.html')
    context = Context(context_dict)
    html = template.render(context)

    wkhtmltopdf_default = 'wkhtmltopdf-heroku'

    # Reference command
    wkhtmltopdf_cmd = os.environ.get('WKHTMLTOPDF_CMD', wkhtmltopdf_default)
    # wkhtmltopdf_cmd = wkhtmltopdf_default

    # Set up return file
    pdf_file = NamedTemporaryFile(delete=False, suffix='.pdf')

    # Save the HTML to a temp file
    html_file = NamedTemporaryFile(delete=False, suffix='.html')
    html_file.write(html)
    html_file.close()

    # wkhtmltopdf
    command = [
        wkhtmltopdf_cmd,
        '-q',
        '--footer-right', '[page]',
        '--footer-font-name', 'Times',
        '--margin-bottom', '1in',
        '--margin-left', '1in',
        '--margin-right', '1in',
        '--margin-top', '1in',
        '--disable-smart-shrinking',
        html_file.name,
        pdf_file.name
    ]

    call_subprocess(command)

    return pdf_file
Exemple #7
0
def generate_pdf(syllabus):
    context_dict = syllabus.json_data
    context_dict['college'] = College.objects.get(pk=context_dict['college'])
    context_dict['department'] = Department.objects.get(
        pk=context_dict['department'])
    context_dict['rubric'] = Rubric.objects.get(
        pk=context_dict['rubric']).json_data

    template = get_template('syllabus/pdf.html')
    context = Context(context_dict)
    html = template.render(context)

    wkhtmltopdf_default = 'wkhtmltopdf-heroku'

    # Reference command
    wkhtmltopdf_cmd = os.environ.get('WKHTMLTOPDF_CMD', wkhtmltopdf_default)
    # wkhtmltopdf_cmd = wkhtmltopdf_default

    # Set up return file
    pdf_file = NamedTemporaryFile(delete=False, suffix='.pdf')

    # Save the HTML to a temp file
    html_file = NamedTemporaryFile(delete=False, suffix='.html')
    html_file.write(html)
    html_file.close()

    # wkhtmltopdf
    command = [
        wkhtmltopdf_cmd, '-q', '--footer-right', '[page]',
        '--footer-font-name', 'Times', '--margin-bottom', '1in',
        '--margin-left', '1in', '--margin-right', '1in', '--margin-top', '1in',
        '--disable-smart-shrinking', html_file.name, pdf_file.name
    ]

    call_subprocess(command)

    return pdf_file