[reportlab-users] Issues related to setting parameters of graphs, lines, and rectangles, and they are not printing correctly

Robin Becker robin at reportlab.com
Wed Dec 5 12:49:43 EST 2012


On 03/12/2012 08:52, Andy Robinson wrote:
...........

> We have actually implemented 'screen only' buttons in some of our

> commercial solutions (e.g. "Buy Now" buttons on a product factsheet,

> which make absolutely no sense when printed), but it's done in a

> completely separate drawing layer. When Robin gets in, hopefully he

> can post a code snippet to do it.

......

We have used code like this to implement forms which appear differently in print.


Attached is an example script which creates a form, uses a non-printing version
of the form and also one that is printed.


--
Robin Becker
-------------- next part --------------
from reportlab.lib.colors import toColor
def makeForm(canv,
text='Hello Cruel World!',
formName='dingo',
width=100,height=30,
fillColor0=toColor('pink'),
fillColor1=toColor('red'),
textColor=toColor('blue'),
strokeWidth=1,
strokeColor=toColor('green'),
fontName='Helvetica',
fontSize=12,
baseline=7,
x0=0,
y0=0,
):
hsw = strokeWidth*0.5
if not canv.hasForm(formName):
from reportlab.pdfbase.pdfmetrics import stringWidth
canv.beginForm(formName,0,0,width+strokeWidth,height+strokeWidth)
canv.setFillColor(fillColor0)
canv.setStrokeColor(strokeColor)
canv.setLineWidth(strokeWidth)
canv.rect(hsw,hsw,width,height,fill=1,stroke=1)
canv.setFillColor(fillColor1)
canv.rect(2*strokeWidth,strokeWidth,width-2*strokeWidth,height-2*strokeWidth,fill=1,stroke=0)
canv.setFillColor(textColor)
sw = stringWidth(text,fontName,fontSize)
swm = width-(2+hsw)*2
if sw>=swm: fontSize *= float(swm)/sw
canv.setFont(fontName,fontSize)
canv.drawCentredString(hsw+width/2.,baseline+hsw,text)
canv.endForm()
return formName

def nAppearance(canv,formName,
width=100,height=30,
strokeWidth=1,
x0=0,
y0=0,
):
from reportlab.pdfbase.pdfdoc import PDFDictionary, PDFObjectReference
hsw = strokeWidth*0.5
rect = (x0-hsw,y0-hsw,x0+width+hsw,y0+height+hsw)
canv.freeTextAnnotation('','()',rect,relative=1,
AP=PDFDictionary(dict(N=PDFObjectReference(canv._doc.getXObjectName(formName)))))
canv._formsinuse.append(formName)


if __name__=='__main__':
from reportlab import rl_config
rl_config.pageCompression = 0
rl_config.invariant = 1
from reportlab.pdfgen.canvas import Canvas
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import registerFont
canv = Canvas('test-freetext.pdf')
formName = makeForm(canv)

#this version of the form won't print
canv.saveState()
canv.translate(72,72)
nAppearance(canv,formName)
canv.drawString(105,0,'Sample of the N Appearance Stream Form XObject')
canv.restoreState()


#this version will
canv.saveState()
canv.translate(72,144)
canv.doForm(formName)
canv.drawString(105,0,'Sample of the standard Form XObject')
canv.restoreState()

canv.showPage()
canv.save()


More information about the reportlab-users mailing list