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
Answers
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
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.
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
Import required Modules