Code snippets are bits of re-usable code submitted by the ReportLab community. We don't promise that they are accurate, up-to-date or correct - use them at your own risk!

We'd love it if you could participate by submitting your own snippets and sharing your experience with others by commenting. You will need to create a user account by filling out our very simple form.

View all snippets


Alpha Colours (transparency for cmyk)
opensourcerl-toolkit

RLIMG: alpha colours (transparency for cmyk)
Author:
rptlab
Posted:
Dec. 18, 2009
Language:
Python
Tags:
opensource rl-toolkit

Although we don't really understand transparency when printing is being carried out. It seems PDF will handle calls for transparency even when using CMYK colors.

"""
A simple usage of alpha (transparent) CMYK colours
"""
from reportlab.graphics.shapes import Rect
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.colors import PCMYKColor
filename = 'not-snip9.pdf'
red = PCMYKColor(0,100,100,0)
black = PCMYKColor(0,0,0,100)
blue = PCMYKColor(100,100,0,0)
red50transparent = red.clone(alpha=50)
c = Canvas(filename,pagesize=(400,200))
c.setFillColor(black)
c.setFont('Helvetica', 10)
c.drawString(25,180, 'solid')
c.setFillColor(blue)
c.rect(25,25,100,100, fill=True, stroke=False)
c.setFillColor(red)
c.rect(100,75,100,100, fill=True, stroke=False)
c.setFillColor(black)
c.drawString(225,180, 'transparent')
c.setFillColor(blue)
c.rect(225,25,100,100, fill=True, stroke=False)
c.setFillColor(red50transparent)
c.rect(300,75,100,100, fill=True, stroke=False)
c.save()