我將matplotlib中的一些圖保存爲pdf格式,因爲它似乎提供了更好的質量。如何使用ReportLab將PDF圖像包含到PDF文檔中?便捷方法Image(文件路徑)不適用於此格式。使用ReportLab(Python)的PDF文檔中的PDF圖像
謝謝。從reportlab.graphics
我將matplotlib中的一些圖保存爲pdf格式,因爲它似乎提供了更好的質量。如何使用ReportLab將PDF圖像包含到PDF文檔中?便捷方法Image(文件路徑)不適用於此格式。使用ReportLab(Python)的PDF文檔中的PDF圖像
謝謝。從reportlab.graphics
採用進口renderPDF
根據ReportLab's FAQ這是唯一可能與ReportLab的PLUS:
我可以使用矢量圖形在我的PDF文件?
不,開源軟件包不會做 這個。的PageCatcher(見前面的 答案),讓您輕鬆 通過保存 爲PDF,然後用它究竟 ,你會映像文件包含任何矢量圖像,並報告 標記語言接受PDF文件 以JPG一起, GIF和PNG。
更新:我沒有研究過這一段時間,但on the page of pdfrw它說:
pdfrw可以讀寫PDF文件,也可以用來讀取PDF中其可以然後在reportlab中使用。
這是正確的。在pdfrw頁面以及一些[questions](http://stackoverflow.com/questions/31712386/loading-matplotlib-object-into-reportlab/)中有幾個使用pdfrw的例子 – 2015-08-16 02:43:34
您可以ReportLab的一起運用之妙pdfrw包,並用它來傳遞的matplotlib人物類文件對象直接進入一個流動性:
這是回答過,但我想給一個小例子,在這裏,還請看看這裏:https://stackoverflow.com/a/13870512/4497962
from io import BytesIO
import matplotlib.pyplot as plt
from pdfrw import PdfReader, PdfDict
from pdfrw.buildxobj import pagexobj
from pdfrw.toreportlab import makerl
from reportlab.platypus import Flowable
from reportlab.lib.enums import TA_JUSTIFY,TA_LEFT,TA_CENTER,TA_RIGHT
class PdfImage(Flowable):
"""
PdfImage wraps the first page from a PDF file as a Flowable
which can be included into a ReportLab Platypus document.
Based on the vectorpdf extension in rst2pdf (http://code.google.com/p/rst2pdf/)
This can be used from the place where you want to return your matplotlib image
as a Flowable:
img = BytesIO()
fig, ax = plt.subplots(figsize=(canvaswidth,canvaswidth))
ax.plot([1,2,3],[6,5,4],antialiased=True,linewidth=2,color='red',label='a curve')
fig.savefig(img,format='PDF')
return(PdfImage(img))
"""
def __init__(self, filename_or_object, width=None, height=None, kind='direct'):
# If using StringIO buffer, set pointer to begining
if hasattr(filename_or_object, 'read'):
filename_or_object.seek(0)
#print("read")
self.page = PdfReader(filename_or_object, decompress=False).pages[0]
self.xobj = pagexobj(self.page)
self.imageWidth = width
self.imageHeight = height
x1, y1, x2, y2 = self.xobj.BBox
self._w, self._h = x2 - x1, y2 - y1
if not self.imageWidth:
self.imageWidth = self._w
if not self.imageHeight:
self.imageHeight = self._h
self.__ratio = float(self.imageWidth)/self.imageHeight
if kind in ['direct','absolute'] or width==None or height==None:
self.drawWidth = width or self.imageWidth
self.drawHeight = height or self.imageHeight
elif kind in ['bound','proportional']:
factor = min(float(width)/self._w,float(height)/self._h)
self.drawWidth = self._w*factor
self.drawHeight = self._h*factor
def wrap(self, availableWidth, availableHeight):
"""
returns draw- width and height
convenience function to adapt your image
to the available Space that is available
"""
return self.drawWidth, self.drawHeight
def drawOn(self, canv, x, y, _sW=0):
"""
translates Bounding Box and scales the given canvas
"""
if _sW > 0 and hasattr(self, 'hAlign'):
a = self.hAlign
if a in ('CENTER', 'CENTRE', TA_CENTER):
x += 0.5*_sW
elif a in ('RIGHT', TA_RIGHT):
x += _sW
elif a not in ('LEFT', TA_LEFT):
raise ValueError("Bad hAlign value " + str(a))
#xobj_name = makerl(canv._doc, self.xobj)
xobj_name = makerl(canv, self.xobj)
xscale = self.drawWidth/self._w
yscale = self.drawHeight/self._h
x -= self.xobj.BBox[0] * xscale
y -= self.xobj.BBox[1] * yscale
canv.saveState()
canv.translate(x, y)
canv.scale(xscale, yscale)
canv.doForm(xobj_name)
canv.restoreState()
您可以從matplotlib使用SVG出口和使用svglib
Python庫包括矢量圖形生成的ReportLab的PDF文件。 svglib
需要一個svg文件並製作一個可以直接在reportlab中使用的繪圖對象。
另請參見此問題以瞭解更多詳細信息:Generating PDFs from SVG input
這是不正確的。 renderPDF獲取EXISTING文檔並生成PDF。與海報想要的完全相反。 – 2011-02-21 16:57:23