py-pdf/fpdf2

How to set text color using CMYK colors?

Closed this issue ยท 5 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? ๐Ÿ™‚

@allcontributors please add @Lucas-C for bug

@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 ๐Ÿ™‚