Python + Flask kullanarak html dosyasından pdf dosyası oluşturmak istiyorum. Bunu yapmak için xhtml2pdf kullanıyorum. İşte benim kodum:Python/Flask kullanarak html dosyasını pdf'ye dönüştürün
def main():
pdf = StringIO()
pdf = create_pdf(render_template('cvTemplate.html', user=user))
pdf_out = pdf.getvalue()
response = make_response(pdf_out)
return response
def create_pdf(pdf_data):
pdf = StringIO()
pisa.CreatePDF(StringIO(pdf_data.encode('utf-8')), pdf)
return pdf
Bu kod dosyasında, anında üretiliyor. FAKAT! xhtml2pdf, sayfayı doğru şekilde işaretlemek için bu büyük sorun nedeniyle CSS'deki birçok stili desteklemez. Başka bir enstrüman buldum (wkhtmltopdf). Ama böyle bir şey yazdığında:
pdf = StringIO()
data = render_template('cvTemplate1.html', user=user)
WKhtmlToPdf(data.encode('utf-8'), pdf)
return pdf
dirilmişti hata:
AttributeError: 'cStringIO.StringO' object has no attribute 'rfind'
Ve benim sorum Flask (anında dosya oluşturulurken birlikte) wkhtmltopdf kullanarak pdf html dönüştürmek nasıl?
Yanıtlarınız için şimdiden teşekkür ederiz.
Tamam.Bu kod gayet iyi çalışıyor (install wkhtmltopdf ile sorunlar hariç): rendered_template = render_template ('template.html', kullanıcı = kullanıcı) rendered_template = rendered_template.encode ('utf-8') pdf = pdfkit.from_string (rendered_template, False, css = './static/styles.css') dönüş make_response (pdf) Teşekkürler! –