python - pdfkit 使用 from_string 创建页面

标签 python pdfkit python-pdfkit

我正在使用 pdfkit 从字符串生成 pdf 文件。

询问:我传递给pdfkit的每个字符串我都希望它作为输出pdf中的一个新页面。

我知道使用如下 from_file 是可能的。但是,我不想将它写入文件并使用它。
pdfkit.from_file(['file1', 'file2'], 'output.pdf')这将创建 2 页的 output.pdf 文件。

类似下面的东西可能吗?

pdfkit.from_string(['ABC', 'XYZ'], 'output.pdf') 

它应该在 output.pdf 文件的第 1 页和第 2 页中写“ABC”和“XYZ”

最佳答案

我知道这已经过时了,但如果其他人发现自己在这里,我想我会分享我所做的。我必须实现 PyPDF2 来执行上面指定的操作。我的类似解决方案是:

from PyPDF2 import PdfFileReader, PdfFileWriter
import io


with open('output.pdf', 'wb') as output_file:
    writer = PdfFileWriter()
    for s in ['ABC', 'XYZ']:
        stream = io.BytesIO()
        stream.write(pdfkit.from_string(s, False))
        # This line assumes the string html (or txt) is only 1 page.
        writer.addPage(PdfFileReader(stream).getPage(0))
    writer.write(output_file)

关于python - pdfkit 使用 from_string 创建页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43464338/

相关文章:

python - Anaconda更新与pip更新和conda更新冲突,anaconda中安装的pip好像坏了

HTML 到 PDF rails

ios - 如何将 IBOutlet 标记为可用于特定的 iOS 版本

ios - 使用 Swift 5 - PDFKit 在 iOS 中编辑和保存现有的 pdf 文档

wkhtmltopdf - FileNotFoundError : [Errno 2] No such file or directory: 'which' when using python pdfkit

python - 如何获取引发异常的方法?

python - 基于FFT的音频分类

python - 如何将二维数组拆分为较小的可变大小的二维数组?