python - 从 python 创建 pdf

标签 python pdf eps

我希望从 Python 应用程序生成 PDF。 它们开始时相对简单,但有些可能会变得更复杂(本质上是像文档一样的字母,但稍后会包含水印等)

我之前在原始后记中工作过,如果我可以生成正确的标题等,并在它的末尾生成文件,我想避免使用可能无法完全满足我的要求的复杂库。有些似乎有位腐烂并且不再受支持(pypdf 和 pypdf2),尤其是当我知道 PDF/Postscript 可以完全满足我的需要时。 PDF 内容确实没有那么复杂。

只需将适当的文本标题写入文件和我的后记代码,我就可以很好地生成 EPS(封装后记)。但是检查 PDF 有一个 lil 二进制 header ,我不确定如何生成。

我可以生成 EPS 并将其转换。我对此不太满意,因为生产环境是 Windows 2008 服务器(开发是 Ubuntu 12.04)并且制作一些东西并转换它看起来很愚蠢。

有人做过吗? 我不想使用图书馆是不是太迂腐了?

最佳答案

borrowed from ask.yahoo

A PDF file starts with "%PDF-1.1" if it is a version 1.1 type of PDF file. You can read PDF files ok when they don't have binary data objects stored in them, and you could even make one using Notepad if you didn't need to store a binary object like a Paint bitmap in it.

But after seeing the "%PDF-1.1" you ignore what's after that (Adobe Reader does, too) and go straight to the end of the file to where there is a line that says "%%EOF". That's always the last thing in the file; and if that's there you know that just a few characters before that place in the file there's the word "startxref" followed by a number. This number tells a reader program where to look in the file to find the start of the list of items describing the structure of the file. These items in the list can be page objects, dictionary objects, or stream objects (like the binary data of a bitmap), and each one has "obj" and "endobj" marking out where its description starts and ends.

For fairly simple PDF files, you might be able to type the text in just like you did with Notepad to make a working PDF file that Adobe Reader and other PDF viewer programs could read and display correctly.

Doing something like this is a challenge, even for a simple file, and you'd really have to know what you're doing to get any binary data into the file where it's supposed to go; but for character data, you'd just be able to type it in. And all of the commands used in the PDF are in the form of strings that you could type in. The hardest part is calculating those numbers that give the file offsets for items in the file (such as the number following "startxref").

If the way the file format is laid out intrigues you, go ahead and read the PDF manual, which tells the whole story. http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/PDF32000_2008.pdf

但实际上你应该只使用一个库

感谢@LukasGraf 提供此链接 http://www.gnupdf.org/Introduction_to_PDF这展示了如何从头开始创建一个简单的 hello world pdf

关于python - 从 python 创建 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20709227/

相关文章:

python - 模块未找到错误: No module named 'torch'

php - 从 php 生成 PDF 报告

image - EPS文件中%%BoundingBox和%ImageData的确切含义是什么

python - 使用离线绘图将 jupyter notebook 导出为 pdf;缺少图表

python - 给定一个列表字典,是否有一种 pythonic 智能方法来比较每个列表的第 i 个元素并提取最大值?

html - 链接/嵌入到 pdf 文档的特定页面/部分?

c# - 将 PDF 文件从 Fiddler 上传到 WebAPI 方法导致 415 Unsupported Media Type

php - 将分层 SVG 矢量图形转换为分层 EPS 矢量图形

r - 由 Sweave/R 生成的数字以及为何仅使用 (PDF)LaTeX

python - 将单元测试与 Django 结合使用的最佳方法