根据您的PDF生成器的功能,图像可以保存为弱,无损或甚至不压缩。您可以使用以下方法从PDF中提取图像信息,以检查这是否属于您的情况。如果是这样,可以使用一些“PDF compression”软件来解决这个问题。
(这可能看起来很奇怪,但我真的没有发现任何预先编写的软件可以做到这一点)
安装Python 2.x和PDFMiner包(参见PDFMiner manual#cmap安装步骤),然后使用以下代码列出文档中的所有图像,它们的大小和压缩。有关PDF使用的压缩算法列表和说明,请参阅PDF specification,第23页(“标准过滤器”表)。
from pdfminer.pdfparser import PDFParser, PDFDocument
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfdevice import PDFDevice
# Open a PDF file.
fp = open('Reader.pdf', 'rb')
# Create a PDF parser object associated with the file object.
parser = PDFParser(fp)
# Create a PDF document object that stores the document structure.
doc = PDFDocument()
# Connect the parser and document objects.
parser.set_document(doc)
doc.set_parser(parser)
# Supply the password for initialization.
# (If no password is set, give an empty string.)
doc.initialize('')
# Check if the document allows text extraction. If not, abort.
if not doc.is_extractable:
raise PDFTextExtractionNotAllowed
# Create a PDF resource manager object that stores shared resources.
rsrcmgr = PDFResourceManager()
from pdfminer.layout import LAParams, LTImage
from pdfminer.converter import PDFPageAggregator
# Set parameters for analysis.
laparams = LAParams()
# Create a PDF page aggregator object.
device = PDFPageAggregator(rsrcmgr, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)
#Build layout trees of all pages
layouts=[]
for page in doc.get_pages():
interpreter.process_page(page)
# receive the LTPage object for the page.
layouts.append(device.get_result())
#search the trees for images and show their info,
# excluding repeating ones
known_ids=set()
count=0;size=0
def lsimages(obj):
global count; global size
if hasattr(obj,'_objs'):
for so in obj._objs:
if isinstance(so,LTImage):
i=so; id=i.stream.attrs['ID'].objid
if id not in known_ids:
a=i.stream.attrs
print a
count+=1;size+=a.get('Length',0)
known_ids.add(id)
lsimages(so)
for l in layouts:
lsimages(l)
print "Total: %d images, %d bytes"%(count,size)
Credits:样板代码取自Programming with PDFMiner文章。
不知道rdlc是什么。但我认为,以PDF格式呈现时,15KB图像不一定必须是15KB。这是因为为web制作的典型图像的分辨率为72dpi。当包含在PDF中时,软件通常会将其转换为200-300dpi以获得最佳的打印质量。一张100x100像素的图像因此在200dpi时变成〜278x278px的图像; 10,000px图像被转换为77,000px,你做数学。 – 2012-03-05 14:39:32
由于没有添加新信息,PDF渲染器保存上采样图像将是愚蠢的。上采样可以等到打印时间。但大量的软件确实愚蠢的事情... – japreiss 2012-03-05 14:46:22
你能告诉我你的图像类型(JPG,PNG,TIF),它的颜色深度(1bpp,8bpp,24bpp等)及其大小(宽度和高度像素)? – iPDFdev 2012-03-05 15:17:21