html template to pdf with images

前端 未结 1 896
栀梦
栀梦 2021-01-17 06:51

I am using django and trying to render invoice pdf, pdf is generating successfully but images are not in pdf.

Actually I want to add barcode image, so I dynamically

1条回答
  •  离开以前
    2021-01-17 07:23

    To get the svg rendered in the html, you have to get the svg text as it is (text output) and put it in your template without using the tag.

    
        
    PLAI73664PJHS

    In the snippet you see the html already rendered but you need to find the way of getting that text and pass it to the template as a context variable, like you are doing with package_no_barcode.

    EDIT

    I've made a test using jinja2 and using flask but the solution should be similar with django and its template engine.

    First, make sure you get the svg code as a string in your server code. Then, pass it to the template as a context variable as I mentioned before.

    In the template, you need to "evaluate" the html or in this case svg code that comes in the variable you passed before. To do that using jinja2, I used the safe filter.

    
        
    {{ barcode | safe }}

    And as a result I got:

    But using django's default template engine I think you need to use the escape builtin filter to get something similar.

    Similar question but using flask and jinja2: Passing HTML to template using Flask/Jinja2

    0 讨论(0)
提交回复
热议问题