Pyppeteerbased异步python包装器,用于将html转换为pdf

pdfmate的Python项目详细描述


PyPIPyPI versionCode style: black

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

学分

这是PDFGen Python和Python PDFKit库的改编版,非常感谢它们!在

其他项目

有什么好处吗?

Yes.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java Spring启动启用HTTPS   actionscript 3 java中的这个[“var”+“name”]   java只匹配给定集合中一个字符的一个匹配项   java Hibernate:防止角色表中出现多个相同的条目   javajersey+Spring注入servlet请求   java HtmlEditor javafx失去焦点   java Apache Wicket AjaxRequestTarget ListView组件未刷新或更新   mysql java。无法将lang.String转换为java。sql。时间戳   java将巨大的整数文件(在一行中)拆分为具有内存限制的已排序块   安卓如何完全关闭proguard?   安装Eclipse和Android SDK后的java“无AVD可用”消息   java动态显示图像视图   java在Spring中还有哪些WebsocketClient实现?   java Glassfish需要很长时间才能重新启动   使用Java简单串行连接器将pc与arduino连接   java如何在camel文件组件配置中结合readLockCheckInterval和maxMessagesPerPoll?   单击Android时的java预览图像   java如何将字节数组转换为ByteArrayOutputStream