【问题标题】:how to display images in pdf using xhtml2pdf in django如何在 django 中使用 xhtml2pdf 以 pdf 格式显示图像
【发布时间】:2019-08-11 18:30:22
【问题描述】:

我正在处理一个 django 项目,我想生成一个 pdf 文件,其中将显示用户的信息,页眉和页脚必须是图像,我使用 xhtml2pdf 库创建 pdf 文件,我已经在某处读到该库不使用“真正的”css 规则。所以图像必须是pdf格式,我尝试转换它但它仍然没有显示。 这是我的看法:

class attestation_travail(View):
    def get(self, request, *args, **kwargs):
        template = get_template('personnel/attestation_travail.html')
        context = {
            'url': static('images/1.pdf')
        }
        html = template.render(context)
        pdf = render_to_pdf('personnel/attestation_travail.html', context)
        if pdf:
            response = HttpResponse(pdf, content_type='application/pdf')
            n = random.randint(100, 100000)
            filename = "Attestation_travail_%s.pdf" %n
            content = "inline; filename='%s'" %(filename)
            download = request.GET.get("download")
            if download:
                content = "attachment; filename='%s'" %(filename)
            response['Content-Disposition'] = content
            return response
        return HttpResponse("Not found")

这是我的 html 文件:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Attestation de travail</title>
        <style type="text/css">
            body {
                font-weight: 200;
                font-size: 14px;
            }
            .header {
                font-size: 20px;
                font-weight: 100;
                text-align: center;
                color: #007cae;
            }
            .title {
                font-size: 22px;
                font-weight: 100;
               /* text-align: right;*/
               padding: 10px 20px 0px 20px;
            }
            .title span {
                color: #007cae;
            }
            .details {
                padding: 10px 20px 0px 20px;
                text-align: left !important;
                /*margin-left: 40%;*/
            }
            .hrItem {
                border: none;
                height: 1px;
                /* Set the hr color */
                color: #333; /* old IE */
                background-color: #fff; /* Modern Browsers */
            }
        </style>
    </head>
    <body>
        <div class='wrapper'>
            <div class='header'>
                <img src="{{ url }}">
            </div>
        </div>
        <div class='details'>

            <hr class='hrItem' />
        </div>
    </div>
    </body>
</html>

【问题讨论】:

    标签: django pdf


    【解决方案1】:

    希望我已经很好地理解了您的问题,我所做的是创建一条返回图像的路线,例如:

    class MyImage(APIView):
        def get(self, request):
                return HttpResponse(myImageFile, content_type="image/png")
    

    在我的 urls.py 文件中 Id 有:

    urlpatterns = [
        path('myImageUrl',views.MyImage.as_view()),
    ]
    

    然后在我的模板中,我有:

    <img src="http://127.0.0.1:8000/myFullPath/myImageUrl" alt="no trend graph found" style=" width: 100%;z-index: 0;height:185px;">
    

    x-index 和 height 和 width 样式是为了帮助图像看起来整洁。

    希望对你有帮助:-)

    【讨论】:

      【解决方案2】:

      HTML 和 Django version=2.0 中的以下代码行对我有用。

      &lt;img src="{{institute_obj.institute_pic.path}}" height="100px"&gt;

      这里的 src 属性替换为你的代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        • 2013-07-03
        • 1970-01-01
        • 1970-01-01
        • 2019-12-20
        • 2013-02-15
        • 1970-01-01
        相关资源
        最近更新 更多