【问题标题】:Django easy_pdf remove marginDjango easy_pdf 删除边距
【发布时间】:2020-05-29 00:24:42
【问题描述】:

要从 html 创建 pdf,我使用 easy_pdf 连接到 xhtml2pdf

小例子:

在 view.py 中

from easy_pdf.rendering import render_to_pdf_response

context = dict({'user_company': 'test'})
template_name = "pdf_template.html"
return render_to_pdf_response(request, template_name, context)

html模板:

<!DOCTYPE html>
{% load static %}

<html lang="en">
<head>
    <style>
        * {
            margin: 0 !important;
            padding: 0 !important;
          }
    </style>
</head>
<body>
    {{user_company}}
    <img src="/static/some_image.png" height="1000" width="1000">
</body>
</html>

图像和所有元素都放在边距内:

我尝试在side_packages/easy_pdf/templates/easy_pdf/base.html 中更改并添加一些边距选项:github link

        <style type="text/css">
            @page {

                // added
                margin: 0 !important;

                * {
                    margin: 0 !important;
                    padding: 0 !important;
                }

                body {
                   margin: 0 !important;
                   padding: 0 !important;
                }


                @frame {
                   margin: 0 !important;
                }

                // changed
                size: {{ pagesize|default:"A4" }};
                margin-left: 0;
                margin-right: 0;
                margin-top: 0;
                margin-bottom: 0;

                @frame header {
                    -pdf-frame-content: page-header;
                    margin-top: 0;
                    margin-right: 0;
                    margin-bottom: 0;
                    margin-left: 0;
                }

                @frame footer {
                    -pdf-frame-content: page-footer;
                    bottom: 0;
                    margin-left: 0;
                    margin-right: 0;
                    height: 0;
                }
            }
        </style>

但没有效果

问题

在哪里添加或更改 css 选项以从 pdf 中删除边距?

【问题讨论】:

    标签: python css django python-3.x django-templates


    【解决方案1】:

    我找到了解决办法:

    您需要在模板样式中添加以下内容:

    <style>
        @page {
                * {
                    margin: 0;
                    padding: 0;
                }
            }
    </style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-25
      • 1970-01-01
      • 2013-06-18
      • 2017-03-11
      • 2017-02-22
      • 1970-01-01
      • 2014-02-27
      • 2016-03-20
      相关资源
      最近更新 更多