Pyppeteerbased异步python包装器,用于将html转换为pdf
pdfmate的Python项目详细描述
PDF日期
pyppeeer的异步/同步包装
安装
pip install pdfmate
使用
对于简单的异步任务:
^{pr2}$同步API也在pdfmate.sync
上为上述所有函数提供:
importpdfmatepdfmate.sync.from_url('http://google.com','out.pdf')pdfmate.sync.from_file('test.html','out.pdf')pdfmate.sync.from_string('Hello!','out.pdf')
您可以传递包含多个URL或文件的列表:
pdfmate.sync.from_url(['google.com','yandex.ru','engadget.com'],'out.pdf')pdfmate.sync.from_file(['file1.html','file2.html'],'out.pdf')
也可以传递打开的文件:
withopen('file.html')asf:pdfmate.sync.pdfmate(f,'out.pdf')
如果您希望进一步处理生成的PDF,可以将其读入 变量:
# Ignore output_path parameter to save pdf to a variablepdf=pdfmate.sync.from_url('http://google.com')
您可以指定用于保存PDF的所有Pyppeteer options,如下所示:
options={'scale':2.0,'format':'Letter','margin':{'top':'0.75in','right':'0.75in','bottom':'0.75in','left':'0.75in',},'pageRanges':'1-5,8',}pdfmate.sync.from_url('http://google.com','out.pdf',options=options)
您还可以通过HTML中的meta标记传递任何选项:
body=""" <html> <head> <meta name="pdfmate-format" content="Legal"/> <meta name="pdfmate-landscape" content="False"/> </head> Hello World! </html> """pdfmate.sync.from_string(body,'out.pdf')
配置
每个API调用都使用一个可选的options参数来配置打印PDF行为。但是,为了减少冗余,可以将默认配置设置为用于所有API调用。它需要 配置选项作为初始参数。可用选项包括:
options
-pyppeeerpage.pdf(options)
调用默认使用的dict。options
作为参数传递给API调用将优先于默认选项。在meta_tag_prefix
-特定于pdfmate
的元标记的前缀-by 默认值是pdfmate-
。在environ
-用于向pyppeeer headless browser提供env变量的dict。在
importpdfmatepdfmate.configuration(options={'format':'A4'})asyncdeff():# The resultant PDF at 'output_file' will be in A4 size and 2.0 scale.awaitpdfmate.from_string(html_string,output_file,options={'scale':2.0})
开发设置
poetry install -v --no-root
运行eh3测试>poetry run pytest tests/
使用lint staged启用git钩子
npx mrm lint-staged
npx husky install
学分
poetry run pytest tests/
npx mrm lint-staged
npx husky install
这是PDFGen Python和Python PDFKit库的改编版,非常感谢它们!在
其他项目
有什么好处吗?
- 项目
标签: