生成 ODT/DOC(X) 并转换为 PDF,无需 OO.o/MS

发布于 2024-08-13 17:41:35 字数 536 浏览 1 评论 0原文

我有一个 WSGI 应用程序,可以生成发票并将其存储为 PDF。

到目前为止,我已经用 FPDF(或等效物)解决了类似的问题,像 GUI 一样从头开始生成 PDF。遗憾的是,这意味着整个格式化逻辑(定位页眉、页脚和内容、样式)都位于应用程序中,而它实际上不应该位于其中。

由于模板已经以 Office 格式(ODT、DOC、DOCX)存在,我更愿意简单地使用这些作为基础并填写实际内容。我发现了 Appy 框架,它可以用带注释的 ODT 文件完成大部分工作。

不过,这仍然留下了一个更大的问题:将 ODT(或 DOC、或 DOCX)转换为 PDF。在服务器上。运行Linux。没有 GUI 库。因此,无需 OO.o 或 MS Office。

这是可能的还是我最好将样式保留在代码中?

实际要填写的内容实际上非常有限:几个段落,其中一些可能是可选的,一两个标题,总是在同一位置,以及表格的几行。在 HTML 中这将是微不足道的。

编辑:基本上,我想要一个可以从充当模板的 ODF 文件生成 ODT 文件的库,以及一个可以将结果转换为 PDF 的库(这可能是关键)。

I have a WSGI application that generates invoices and stores them as PDF.

So far I have solved similar problems with FPDF (or equivalents), generating the PDF from scratch like a GUI. Sadly this means the entire formatting logic (positioning headers, footers and content, styling) is in the application, where it really shouldn't be.

As the templates already exist in Office formats (ODT, DOC, DOCX), I would prefer to simply use those as a basis and fill in the actual content. I've found the Appy framework, which does pretty much that with annotated ODT files.

That still leaves the bigger problem open, tho: converting ODT (or DOC, or DOCX) to PDF. On a server. Running Linux. Without GUI libraries. And thus, without OO.o or MS Office.

Is this at all possible or am I better off keeping the styling in my code?

The actual content that would be filled in is actually quite restricted: a few paragraphs, some of which may be optional, a headline or two, always at the same place, and a few rows of a table. In HTML this would be trivial.

EDIT: Basically, I want a library that can generate ODT files from ODF files acting as templates and a library that can convert the result into PDF (which is probably the crux).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娜些时光,永不杰束 2024-08-20 17:41:35

我不知道如何进行自动 ODT -> PDF 转换,但更简单的方法可能是生成 HTML 格式的发票,然后使用 http://www.xhtml2pdf.com/" rel="nofollow noreferrer">http://www.xhtml2pdf.com 将其转换为 PDF。 xhtml2pdf.com/。我自己还没有尝试过这个库,但它看起来确实很有前途。

I don't know how to go about automatic ODT -> PDF conversion, but a simpler route might be to generate your invoices as HTML and convert them to PDF using http://www.xhtml2pdf.com/. I haven't tried the library myself, but it definitely seems promising.

夜夜流光相皎洁 2024-08-20 17:41:35

您可以在 PyQt4 中使用 QTextDocument、QTextCursor 和 QTextDocumentWriter。一个简单的例子来展示如何写入 odt 文件:

>>>from pyqt4 import QtGui
# Create a document object
>>>doc = QtGui.QTextDocument()
# Create a cursor pointing to the beginning of the document
>>>cursor = QtGui.QTextCursor(doc)
# Insert some text
>>>cursor.insertText('Hello world')
# Create a writer to save the document
>>>writer = QtGui.QTextDocumentWriter()
>>>writer.supportedDocumentFormats()
[PyQt4.QtCore.QByteArray(b'HTML'), PyQt4.QtCore.QByteArray(b'ODF'), PyQt4.QtCore.QByteArray(b'plaintext')]
>>>odf_format = writer.supportedDocumentFormats()[1]
>>>writer.setFormat(odf_format)
>>>writer.setFileName('hello_world.odt')
>>>writer.write(doc) # Return True if successful
True

如果不确定这种情况下 odt 和 odf 之间的区别。我检查了文件类型,它显示“application/vnd.oasis.opendocument.text”。所以我认为它是odt。您可以使用 QPrinter 打印为 pdf 文件。

更多信息请访问:
http://qt-project.org/doc/qt-4.8/

You can use QTextDocument, QTextCursor and QTextDocumentWriter in PyQt4. A simple example to show how to write to an odt file:

>>>from pyqt4 import QtGui
# Create a document object
>>>doc = QtGui.QTextDocument()
# Create a cursor pointing to the beginning of the document
>>>cursor = QtGui.QTextCursor(doc)
# Insert some text
>>>cursor.insertText('Hello world')
# Create a writer to save the document
>>>writer = QtGui.QTextDocumentWriter()
>>>writer.supportedDocumentFormats()
[PyQt4.QtCore.QByteArray(b'HTML'), PyQt4.QtCore.QByteArray(b'ODF'), PyQt4.QtCore.QByteArray(b'plaintext')]
>>>odf_format = writer.supportedDocumentFormats()[1]
>>>writer.setFormat(odf_format)
>>>writer.setFileName('hello_world.odt')
>>>writer.write(doc) # Return True if successful
True

If not sure the difference between odt and odf in this case. I checked the file type and it said 'application/vnd.oasis.opendocument.text'. So I assume it is odt. You can print to a pdf file by using QPrinter.

More information at:
http://qt-project.org/doc/qt-4.8/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文