Django and weasyprint, merge pdf

后端 未结 2 1560
陌清茗
陌清茗 2021-02-06 10:44

It\'s possible to merge multiple pdf in django with weasyprint?

I have something like this:

def verpdf(request, pk):
    odet = get_object_or_404(Note, p         


        
相关标签:
2条回答
  • 2021-02-06 11:05

    Took me a while, but i solved it, was my fault for not understand the documentation lol, here is the code if anyone have the same problem:

    def verpdf(request, pk):
        odet = get_object_or_404(Note, pk = pk)
        template = get_template('pdfnot.html')
        template1 = get_template('pdfnot2.html')
        p1 = template.render({'odet': odet}).encode(encoding="ISO-8859-1")
        p2 = template1.render({'note':odet}).encode(encoding="ISO-8859-1")
        pdf1 = HTML(string=p1)
        pdf2 = HTML(string=p2)
        pdf11 = pdf1.render()
        pdf12 = pdf2.render()
    
        val = []
    
        for doc in pdf11, pdf12:
            for p in doc.pages:
                val.append(p)
    
        pdf_file = pdf11.copy(val).write_pdf() # use metadata of pdf11
    
        http_response = HttpResponse(pdf_file, content_type='application/pdf')
        http_response['Content-Disposition'] = 'filename="report.pdf"'
    
        return http_response
    

    And with this an pdf output with two pages.

    0 讨论(0)
  • 2021-02-06 11:16
    • Short & simple solution is that
    • Add this <p style="page-break-before: always"></p> after body tag in html file
    • It work for me
    0 讨论(0)
提交回复
热议问题