使用画布绘制图像并使用SimpleDocTemplate [英] Draw images with canvas and use SimpleDocTemplate

查看:693
本文介绍了使用画布绘制图像并使用SimpleDocTemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ p

我正在使用SimpleDocTemplate,它是一个django视图,它们非常简单,标题,内容和页脚。适合很好,在内容中绘制表格,页脚和标题都是使用以下方式显示的:

  build([data] onFirstPage = drawPageFrame,onLaterPages = drawPageFrame)。 

我的问题是,如何绘制像Canvas.drawImage(...)的图像?我需要一个浮动图像,位于我想要的文本上,并且使用SimpleDocTemplate我没有Canvas对象来执行此操作。



Thaks。



搜索我发现这个:


platypus布局的东西使用流动。打包者通常将
属性canv设置到每个可流动的文件上,当它被包装,拆分或
绘制即围绕wrap,split和draw方法。在这些方法中,
您可以使用自己的canv属性访问画布。


¿¿



嗯,更多的东西要测试:

 
flowables.CallerMacro



 
# - * - 编码: utf-8 - * -
from reportlab.lib.pagesizes import A4,landscape,portrait
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Table,Flowable,SimpleDocTemplate,Paragraph ,Spacer,Image
from reportlab.lib import randomtext
from reportlab import platypus

import os,random

styles = getSampleStyleSheet()
path = os.path.realpath(os.path.dirname(__ file__))

def drawPageFrame(canvas,doc):
canvas.saveState()
canvas.drawImage (path +/ ujiPDF.jpg,50,50,57,57)
canvas.restoreState()

doc = SimpleDocTemplate(salida.pdf,pagesize = A4)

eleme ntos = []

com ='canvas.drawImage('+ path +'/ ujiPDF.jpg,100,100,57,57)'
print com
elementos.append (platypus.flowables.Macro('canvas.saveState()'))
print platypus.flowables.Macro(com)
elementos.append(platypus.flowables.Macro(com))
elementos.append(platypus.flowables.Macro('canvas.restoreState()'))

para = Paragraph(randomtext.randomText(randomtext.PYTHON,20),styles [Normal])
elementos.append(para)

doc.build(elementos,onFirstPage = drawPageFrame,onLaterPages = drawPageFrame)

这是宏的aproach ...清除退出,但没有第二个图像....

解决方案

你最好的选择是创建SimpleDocTemplate或BaseDocTemplate的子类。在构建方法中,您将可以访问画布。如果您想了解SimpleDocTemplate所做的一切,您可以尝试直接从 site-packages / reportlab / platypus / doctemplate.py 中复制。


I'm writing pdfs with reportlab inside a django view, they are very simple, the header, the contents and the footer.

I'm using SimpleDocTemplate wich fits very well, to draw tables in the contents, the footer and the header are drwan using:

build([data], onFirstPage=drawPageFrame, onLaterPages=drawPageFrame).

My question is, How can I draw a image like using Canvas.drawImage(...)? I need a "floating" image... positioned over the text where I want, and with SimpleDocTemplate I don't have a Canvas object to do this.

Thaks.

Searching I have found this:

The platypus layout stuff uses flowables. Packers normally set the attribute canv onto each flowable when it is being wrapped, split or drawn ie around the wrap, split and draw methods. Inside those methods you have access to the canvas using the canv attribute of self.

¿¿How can this be used??

Ummmm, more stuff to test:

flowables.Macro
flowables.CallerMacro

# -*- coding: utf-8 -*-
from reportlab.lib.pagesizes import A4, landscape, portrait
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Table, Flowable, SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib import randomtext
from reportlab import platypus

import os,random

styles = getSampleStyleSheet()
path = os.path.realpath(os.path.dirname(__file__))

def drawPageFrame(canvas, doc):
    canvas.saveState()
    canvas.drawImage(path+"/ujiPDF.jpg",50,50,57,57)
    canvas.restoreState()

doc = SimpleDocTemplate("salida.pdf",pagesize=A4)

elementos = []

com = 'canvas.drawImage("'+path+'/ujiPDF.jpg",100,100,57,57)'
print com
elementos.append(platypus.flowables.Macro('canvas.saveState()'))
print platypus.flowables.Macro(com)
elementos.append(platypus.flowables.Macro(com))
elementos.append(platypus.flowables.Macro('canvas.restoreState()'))

para = Paragraph(randomtext.randomText(randomtext.PYTHON,20), styles["Normal"])
elementos.append(para)

doc.build(elementos,onFirstPage=drawPageFrame, onLaterPages=drawPageFrame)

This is the Macro aproach...clean exit but without the second image....

解决方案

Your best option is to create a subclass of SimpleDocTemplate or BaseDocTemplate. In the build method you will have access to the canvas. If you want to tap into everything that SimpleDocTemplate does, you might try copying it directly from site-packages/reportlab/platypus/doctemplate.py.

这篇关于使用画布绘制图像并使用SimpleDocTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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