py-pdf / fpdf2

Simple PDF generation for Python

Home Page:https://py-pdf.github.io/fpdf2/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to set text color using CMYK colors?

swasher opened this issue Β· comments

I want to use CMYK color space for object creation. But FPDF.set_text_color() method accept only RGB values:

def set_text_color(self, r, g=-1, b=-1):
    ...

I found DeviceCMYK class in drawing.py, but have no imagine how apply it to set_text_color and similar functions.

So, is there a way for assign CMYK color for object?

Hi @swasher

Good question.
You SHOULD be able to do this:

from fpdf import FPDF
from fpdf.drawing import DeviceCMYK

pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=40)
pdf.set_text_color(DeviceCMYK(.32, 0, .1, .59))
pdf.cell(text="Hello world!")
pdf.output("repro.pdf")

But currently, with fpdf2 version 2.7.9, you will get this error:
ValueError: too many values to unpack (expected 3)

I have just fixed that bug, and the fix will be included in the next version.

For now, you can simply do this instead:

from fpdf import FPDF
from fpdf.drawing import DeviceCMYK

pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=40)
pdf.text_color = DeviceCMYK(.32, 0, .1, .59)
pdf.cell(text="Hello world!")
pdf.output("repro.pdf")

Does this answer your question @swasher? πŸ™‚

@Lucas-C

I've put up a pull request to add @Lucas-C! πŸŽ‰

Does this answer your question @swasher? πŸ™‚

Hi Lucas! Thank you for your support, now I can use CMYK color without any problem!)

Great! Closing this then πŸ™‚