![](/img/trans.png)
[英]How to save (store) PDF file generated by weasyprint to specified directory?
[英]How to remove margins from PDF? (Generated using WeasyPrint)
我正在嘗試在我的 Flask 應用程序中呈現 PDF 文檔。 為此,我使用以下 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>
我的 header 部分的頂部和右側/左側不斷出現白邊距:
有沒有辦法刪除它們?
編輯:
下面是用於在我的 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)
也許你忘記了“;”或/和“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.