简体   繁体   English

如何从 PDF 中删除边距? (使用 WeasyPrint 生成)

[英]How to remove margins from PDF? (Generated using WeasyPrint)

I am trying to render a PDF document within my Flask application.我正在尝试在我的 Flask 应用程序中呈现 PDF 文档。 For this, I am using the following HTML template:为此,我使用以下 HTML 模板:

<!DOCTYPE html>

<html>
<head>
<style>
    @page {
        margin:0
    }
    h1 {
        color:white;
    }
    .header{
        background: #0a0045;
        height: 250px;
    }
    .center {
        position: relative;
        top: 50%;
        left: 50%;
        -ms-transform: translate(-50%, -50%);
        transform: translate(-50%, -50%);
        text-align:center;
    }

</style>
</head>
<body>
<div class="header">
    <div class="center">
        <h1>Name</h1>
    </div>
</div>
</body>
</html>

I keep getting white margins at the top and right/left of my header section:我的 header 部分的顶部和右侧/左侧不断出现白边距:

如何从此 PDF 中删除边距?

Is there a way to remove them?有没有办法删除它们?

Edit:编辑:

Below is the code used to generate the PDF file using WeasyPrint in my Flask app:下面是用于在我的 Flask 应用程序中使用 WeasyPrint 生成 PDF 文件的代码:

def generate_pdf(id):
    element = Element.query.filter_by(id=id).first()
    attri_dict = get_element_attri_dict_for_tpl(element)
    html = render_template('element.html', attri_dict=attri_dict)
    pdf = HTML(string=html).write_pdf()
    destin_loc = app.config['ELEMENTS_FOLDER']
    timestamp = dt.datetime.now().strftime('%Y%m%d%H%M%S')
    file_name = '_'.join(['new_element', timestamp])
    path_to_new_file = destin_loc + '/%s.pdf' % file_name
    f = open(path_to_new_file, 'wb')
    f.write(pdf)
    filename = return_latest_element_path()
    return send_from_directory(directory=app.config['ELEMENTS_FOLDER'],
                           filename=filename,
                           as_attachment=True)

Maybe you forgot "; " or/and " mm ", it works:也许你忘记了“;”或/和“mm”,它有效:

@page {
        size: A4; /* Change from the default size of A4 */
        margin: 0mm; /* Set margin on each page */
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM