动态生成的PDF文件可用于除Adobe Reader之外的大多数阅读器 [英] Dynamically generated PDF files working in most readers except Adobe Reader

查看:99
本文介绍了动态生成的PDF文件可用于除Adobe Reader之外的大多数阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从用户输入动态生成PDF,基本上是在其中打印用户输入并将其覆盖在未创建的现有PDF上.

I'm trying to dynamically generate PDFs from user input, where I basically print the user input and overlay it on an existing PDF that I did not create.

它起作用,但有一个主要例外.在Windows或Linux上,Adobe Reader无法正确阅读.手机上的QuickOffice也无法读取.所以我以为我会追踪创建文件的路径-

It works, with one major exception. Adobe Reader doesn't read it properly, on Windows or on Linux. QuickOffice on my phone doesn't read it either. So I thought I'd trace the path of me creating the files -

1-背景的原始PDF 用Adobe Distiller制作的具有LZW编码的PDF 1.2.我没做.

1 - Original PDF of background
PDF 1.2 made with Adobe Distiller with the LZW encoding. I didn't make this.

2-背景的PDF
用Ghostscript制作的PDF 1.4.我在上面使用pdf2ps然后是ps2pdf剥离了LZW,以便reportlab和pyPDF库可以识别它.请注意,在Adobe Reader中,此文件看起来像模糊",就像是不良扫描一样,但在其他阅读器中看起来不错.

2 - PDF of background
PDF 1.4 made with Ghostscript. I used pdf2ps then ps2pdf on the above to strip LZW so that the reportlab and pyPDF libraries would recognize it. Note that this file looks "fuzzy," like a bad scan, in Adobe Reader, but looks fine in other readers.

3-将PDF合并为用户输入的文本背景
从用户输入使用Reportlab制作的PDF 1.3.可以正确打开,并且在我尝试过的每个读者中看起来都不错.

3 - PDF of user-input text formatted to be combined with background
PDF 1.3 made with Reportlab from user input. Opens properly and looks good in every reader I've tried.

4-已完成的PDF 由PyPDF在2和3上的mergePage()函数制成的PDF 1.3.

4 - Finished PDF
PDF 1.3 made from PyPDF's mergePage() function on 2 and 3.

无法在以下位置打开:
Windows的Adobe Reader
适用于Linux的Adobe Reader
Android版QuickOffice

Does not open in:
Adobe Reader for Windows
Adobe Reader for Linux
QuickOffice for Android

完美打开于:
网络上的Google文档的PDF查看器
适用于linux的evince
适用于Linux的ghostscript查看器 适用于Windows的Foxit阅读器
Mac版预览

Opens perfectly in:
Google Docs' PDF viewer on the web
evince for linux
ghostscript viewer for linux Foxit reader for Windows
Preview for Mac

我应该知道哪些已知问题?我不确切知道什么是扁平",但是从互联网上我了解到它是LZW的一种PDF压缩的开源替代品吗?那会导致我的问题吗?如果是这样,我是否可以使用任何库来解决代码中的原因?

Are there known issues that I should know about? I don't know exactly what "flate" is, but from the internet I gather that it's some sort of open source alternative to LZW for PDF compression? Could that be causing my problem? If so, are there any libraries I could use to fix the cause in my code?

推荐答案

第一句话:

您的第二步有很多缺点.如果将PDF转换回PostScript,然后再次转换回PDF,则会降低质量.此过程称为重新油炸PDF",而PDF专业人员通常对此并不满意. (原因是:生成的文件可能看起来像模糊",如扫描不良;文件可能丢失了其嵌入的字体;文件可能替换了原始字体;文件肯定丢失了其透明胶片;图像的分辨率改变了;颜色改变了... )

First remark:

Your 2nd step has many, many drawbacks. If you convert PDF back to PostScript and then again back to PDF, you are going to loose quality. This process is called "re-frying PDFs", and is generally being frowned upon on the part of PDF professionals. (The reasons are: resulting files may look "fuzzy", like bad scans; files may have lost their embedded fonts; files may have replaced original fonts; files certainly have lost their transparencies; images have changed resolutions; colors have changed....)

有时候,您除了重新油炸"外别无选择...但是在这里,您要做.

Sometimes you have no other choice than "re-frying"... but here you DO.

如果使用Ghostscript,则可以对PDF文件进行直接的PDF到PDF转换,并且不会发生内部的,隐藏的PostScript转换. (这是Ghostscript很少为人所知的功能,因此,这个答案通常应得到很多好评; -P.)

If you use Ghostscript, you can do a direct PDF-to-PDF conversion of PDF files, and there will be no internal, hidden PostScript conversion happening. (This is a very rarely known feature of Ghostscript, and therefor this answer normall would deserve lots of upvotes ;-P ).

由于您确实想摆脱内部LZW压缩,因此以下是在Ghostscript中执行的操作:

Since you do want to get rid of internal LZW compression, here is how to do it in Ghostscript:

  1. 从Ghostscript源代码存储库下载一个用PostScript语言编写的实用程序: pdfinflt.ps
  2. 运行以下命令行:

  1. Download a little utility program, written in PostScript language, available from the Ghostscript source code repository: pdfinflt.ps
  2. Run the following commandline:

gswin32c.exe -- [c:/path/to/]pdfinflt.ps input.pdf output.pdf

更新:上面的命令行原本是错误的.
我给它的名称是gswin32c.exe -- [c:/path/to/]pdfinflt.ps output.pdf input.pdf.
那是错误的输入和输出顺序.我的错!抱歉.

Update: Above commandline originally was wrong.
I had given it as gswin32c.exe -- [c:/path/to/]pdfinflt.ps output.pdf input.pdf.
That was the wrong order of input and output. My bad! Sorry about that.

生成的PDF将解压缩其所有内部数据流,而不会因重新油炸 PDF ==> PS ==> PDF 而降低质量.

The resulting PDF will have decompressed all its internal data streams, without loosing quality through your PDF ==> PS ==> PDF re-frying.

我认为您应该使用其他工具(即pdftk ***)来完成第4步.这样做的好处是可以完全避免您完全执行步骤1.和2..

I think you should do your 4th step with a different tool, namely pdftk***. This has the advantage of saving you completely from going through steps 1. and 2. altogether.

pdfk(PDF工具包,在此处下载)是一个命令行该实用程序可在Linux,Unix(pdftk)和Windows(pdftk.exe)上使用,它们可以在PDF上做很多事情,包括将两个PDF的页面相互覆盖.这就是我建议您使用的方式. pdftk可以覆盖步骤"3"中的PDF.一口气将原始PDF转换为原始PDF(反之亦然),而无需先删除每个原始文件或将它们删除.

pdfk (PDF ToolKit, download here) is a commandline utility, available on Linux, Unix (pdftk) and Windows (pdftk.exe), which can do a lot of things on PDFs, including overlaying the pages of two PDFs over each other. This is what I'd recommend you to use. pdftk can overlay the PDF from your step "3." to your original PDF (or vice versa) in one go without first needing to de-flate or de-LZW each one.

以下是供您测试的命令:

Here are commands for you to test:

pdftk.exe ^
  original.pdf ^
  background pdf-from-userinput-step3.pdf ^
  output merged.pdf

pdftk.exe ^
  pdf-from-userinput-step3.pdf ^
  background original.pdf ^
  output merged.pdf

pdftk.exe ^
  original.pdf ^
  stamp pdf-from-userinput-step3.pdf ^
  output merged.pdf

pdftk.exe ^
  pdf-from-userinput-step3.pdf ^
  stamp original.pdf ^
  output merged.pdf

您可能会想知道stampbackground命令之间的区别.这些命令的作用与名称相同:将PDF页面排序为前景或背景层.如果两个PDF都具有透明的背景(而不是纯白色的不透明背景),则在许多情况下结果将看起来相同.

You'll probably wonder about the difference between the stamp and background commands. The commands do what their name suggests: order the PDF page into the foreground or the background layer. Should both PDFs have transparent backgrounds (instead of solid white opaque), the result will in many cases be looking the same.

这篇关于动态生成的PDF文件可用于除Adobe Reader之外的大多数阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆