实现HTML转PDF & 多个PDF合并

来源:互联网 发布:男生用什么护肤品知乎 编辑:程序博客网 时间:2024/06/07 05:44

【html转pdf】

目标:实现将一个html文件一键转换成pdf文件
工具: 使用开源工具wkhtmltopdf, 安装简易,命令行调用。
下载链接
https://wkhtmltopdf.org/downloads.html
使用示例

转换指令:
wkhtmltopdf src_html dst_pdf
例:
wkhtmltopdf test.html test.pdf

【pdf合并】

目标: 将多个pdf文件按指定顺序合并成一个pdf
工具: 使用python工具库: PyPDF2
下载链接 :pip install PyPDF2
使用示例
将要合并的pdf文件句柄按顺序放入列表中,实现依次合并。

from PyPDF2 import PdfFileMergerimport osDIR = "dir-with-pdfs/"OUTPUT = "output.pdf"file_list = filter(lambda f: f.endswith('.pdf'), merger = PdfFileMerger(strict=False)for f_name in file_list:  f = open(os.path.join(DIR, f_name), "rb")  merger.append(f)output = open(OUTPUT, "wb")merger.write(output)