PDFBox 中的合并文档要么没有空白页,要么太多...但仅在打印时

发布于 2024-11-30 14:17:09 字数 1498 浏览 1 评论 0原文

我目前正在进行的项目需要大量印刷文件,这些文件将邮寄给客户。例如,单次印刷可能包含 1000 多封信件,每封信件都会发送给不同的客户。

为了生成此印刷品,外部服务(即不使用 PDFBox)首先将这些字母中的每一个生成为单独的 PDF 文件。然后我的代码加载所有这些 PDF 文件,将它们与 PDFBox 合并在一起(以便它们可以在打印室中作为单个作业打印),然后我在前面添加一个“横幅页”(也使用 PDFBox),其中告诉印刷室如何处理这些信件。

所有东西都是双面打印的。但由于每封信都会寄给不同的客户,因此我不能将两封信背靠背打印在一张纸上。这意味着任何页数为奇数的信件都需要添加空白页。就在打印之前,仅包含两个字母的非常小的打印运行的合并 PDF 文档可能看起来像这样:

Banner Page
Blank
Letter 1
Blank
Letter 2
Blank

但这就是我的问题所在。我已经使用此代码添加了空白页:

PDFMergerUtility docMerger = new PDFMergerUtility();
for (PDDocument currDoc : documents) {

    // Add blank page to document if odd number
    if (currDoc.getNumberOfPages() % 2 != 0) {
        currDoc.addPage(new PDPage());
    }

    // Merge into single document
    docMerger.appendDocument(mergedDoc, currDoc);

    // Close currDoc
    currDoc.close();
}

这似乎始终有效对于横幅页,但对于某些类型的字母,它会搞砸。如果我打开生成的 PDF,它看起来不错,但当我打印时,每个字母之间都会出现一张额外的空白纸。

因此,当我在 Acrobat Reader(甚至 PDFBox 的查看器)中看到以下内容时:

Sheet 1: Banner Page
Sheet 2: Letter 1
Sheet 3: Letter 2

打印输出如下所示:

Sheet 1: Banner Page
Sheet 2: Letter 1
Sheet 3: BLANK!
Sheet 4: Letter 2
Sheet 5: BLANK!

同时,如果我取出 new PDPage() 代码,我会得到以下结果: Acrobat 和打印输出:

Sheet 1: Banner Page
Sheet 2: Letter 1 (front) AND Letter 2 (back)

我尝试通过 PDDocument.silentPrint(PrinterJob) 进行调试,但是当它深入到 PDF 的结构并且没有任何内容跳出来时,事情就开始变得非常危险。 地方。知道会发生什么吗?

The project I'm currently working on requires big print runs of documents, which will be mailed out to clients. For instance, a single print run could include over 1000 letters, each of which will go to a different client.

To produce this print run, an external service (i.e. not using PDFBox) first generates each of these letters as an individual PDF file. Then my code loads all of these PDF files, merges them all together with PDFBox (so that they can be printed as a single job in the printing room), and I add a "banner page" to the front (also with PDFBox) which tells the printing room what to do with the letters.

Everything is printed double-sided. But since each letter is going to a different client, I can't have two letters printed back-to-back on a single sheet of paper. That means that any letter with an odd number of pages needs to have a blank page added to it. Just prior to printing, the merged PDF document for a very small print run containing only two letters could look something like this:

Banner Page
Blank
Letter 1
Blank
Letter 2
Blank

But this is where my problem comes in. I've added the blank pages with this code:

PDFMergerUtility docMerger = new PDFMergerUtility();
for (PDDocument currDoc : documents) {

    // Add blank page to document if odd number
    if (currDoc.getNumberOfPages() % 2 != 0) {
        currDoc.addPage(new PDPage());
    }

    // Merge into single document
    docMerger.appendDocument(mergedDoc, currDoc);

    // Close currDoc
    currDoc.close();
}

This seems to work consistently for banner pages, but for some types of letters, it screws up. If I open the resultant PDF, it looks okay, but when I print, I get an extra blank sheet of paper between each letter.

So while I see this in Acrobat Reader (or even PDFBox's viewer):

Sheet 1: Banner Page
Sheet 2: Letter 1
Sheet 3: Letter 2

The printed output looks like this:

Sheet 1: Banner Page
Sheet 2: Letter 1
Sheet 3: BLANK!
Sheet 4: Letter 2
Sheet 5: BLANK!

Meanwhile, if I take that new PDPage() code out, I get the following result, in both Acrobat and printed output:

Sheet 1: Banner Page
Sheet 2: Letter 1 (front) AND Letter 2 (back)

I've tried debugging through PDDocument.silentPrint(PrinterJob), but things start getting really dicey when it gets deep into the PDF's structure and nothing jumps out as being out of place. Any idea what could be going on?

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

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

发布评论

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

评论(1

温柔嚣张 2024-12-07 14:17:09

好吧,我想我可能已经解决了这个问题。看起来有些字母是用“A4”纸设计的,而不是“Letter”。如果我只是在 Acrobat Reader 中查看文件,它看起来非常好,如果我从那里打印字母,Reader 一定会自动将输出压缩到可用的纸张尺寸上。不幸的是,PDFBox 似乎没有做同样的压缩。因此,当通过 PDFBox 打印时,这封信会稍微跑到下一页上,从而导致我看到的奇怪行为。

我还没有机会测试这个理论......信件开发人员发现了这个错误,但他的更改还没有放在我们的测试服务器上。如果这不能解决问题,我会报告回来。

Okay, I think I may have figured the problem out. It looks like some of the letters were designed with "A4" paper, rather than "Letter". If I just looked at the files in Acrobat Reader, it looked perfectly fine, and if I printed the letters from there, Reader must have automatically squeezed the output onto the available paper size. Unfortunately, PDFBox doesn't seem to do the same squeezing. So when printing through PDFBox, the letter would run slightly over onto the next page, causing the weird behaviour I saw.

I haven't had a chance to test this theory yet... the letter developer found the bug, but his changes haven't been put on our testing server yet. If this doesn't fix the problem, I'll report back.

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