0

I am converting HTML to PDF. PDF downloaded success but it return blank pdf pages not showing any converted content. in my HTML file have Shopify CSS links. if convert minimum content HTML file then it converted correctly

from django.shortcuts import render from django.http import HttpResponse import pdfkit from django.conf import settings

def convert_html_to_pdf(request): if request.method == 'POST': rendered_template = render(request, 'newundergrace.html') HTML file

    options = {
        'enable-local-file-access': '',
        'orientation': 'landscape',
        'page-size': 'Letter',
        'page-size': 'A4',
        'margin-top': '0',
        'margin-right': '0',
        'margin-bottom': '0',
        'margin-left': '0',
        'dpi': 96,
    }


    rendered_content = rendered_template.content.decode('utf-8')


    pdf = pdfkit.from_string(rendered_content, False, options=options)  

    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=output.pdf'
    response.write(pdf)
    return response

return render(request, 'index1.html')

2 Answers2

0

Configuring Pisa for Django shouldn't be too hard.

There are really several examples on the net that show you how to do it and explain how to link to external resources in your templates:

http://www.arnebrodowski.de/blog/501-Pisa-and-Reportlab-pitfalls.html django - pisa : adding images to PDF output http://antydba.blogspot.com/2009/12/django-pisa-polskie-czcionki.html http://www.20seven.org/journal/2008/11/pdf-generation-with-pisa-in-django.html

options = {
    'page-size': 'A4',
    'margin-top': '0mm',
    'margin-right': '0mm',
    'margin-bottom': '0mm',
    'margin-left': '0mm',
    'encoding': 'UTF-8'
}

pdfkit.from_file('input.html', 'output.pdf', options=options)

Make sure to replace 'input.html' with the path to your HTML file, and 'output.pdf' with the desired path for the resulting PDF file.

Note: Ensure that the wkhtmltopdf executable is in the system's PATH or provide its path explicitly using the configuration parameter of pdfkit.

Ashish Kumar Saxena
  • 4,400
  • 8
  • 27
  • 48
0

I tried pdfkit, html2pdf and htmltopdf, But I couldn't able to convert html to pdf. I found weasyprint module it works for me.

it required weasyprint to be installed. The install can be downloaded from here. HTML and CSS that can export to PDF.

install required Modules/packages

pip install weasyprint

Import required Modules

from weasyprint import HTML
HTML("c:/xyz/out_case.html").write_pdf("c:/xyz/sample_out.pdf")
thrinadhn
  • 1,673
  • 22
  • 32