Skip to main content
Skip table of contents

pdfHTML: support for CSS4 device-cmyk example

Introduction

Due to popular demand from customers, we have recently added support for adding content made with device-dependent CMYK colors in pdfHTML 4.0.4 by using CSS syntax. We did this by allowing users to define colors using the device-cmyk() function, which is a feature from the CSS Color Module Level 4.

How it works

To create a PDF document with CMYK colors using pdfHTML you'll need to define colors in your CSS with the device-cmyk() function, as shown in the example HTML below:

XML
<p style="color: device-cmyk(70% 0% 100% 40%);">green color text</p>
<p style="color: device-cmyk(70%, 0%, 100%, 40%);">green color text</p>
<p style="color: device-cmyk(0 81% 81% 30% / .5);">red color text</p>
<p style="color: device-cmyk(0 81% 81% 30% / .5 rgb(178 34 34));">red color text</p>

Be aware, this is still an experimental feature in CSS. When you've defined the colors in this way, your browser likely won't show these colors since the CSS feature hasn't yet been implemented in most current browsers.

To allow pdfHTML to use to this CSS feature you'll need to add a method call to your iText code to enable processing. This feature is not supported out of the box since device-cmyk() only exists in CSS working drafts. Instead, we've added the CssDeclarationValidationMaster#setValidator static method (Java/.NET) specifically to handle this and potentially other new CSS features we might add support for in the future. For this feature, you will need to add a new CssDeviceCmykAwareValidator to the static method to enable processing.

The minimal iText code required to make this feature work would look like this for Java:

JAVA
    public void run(String inputPath,String outputPath) throws IOException {
        CssDeclarationValidationMaster.setValidator(new CssDeviceCmykAwareValidator());//Add this static method call before conversion
        HtmlConverter.convertToPdf(Files.newInputStream(Paths.get(inputPath)),Files.newOutputStream(Paths.get(outputPath)));
    }


And for C# it would be the following: 

C#
    public void Run(string inputPath, string outputPath)
    {
        CssDeclarationValidationMaster.SetValidator(new CssDeviceCmykAwareValidator()); //Add this static method call before conversion
        HtmlConverter.ConvertToPdf(new FileStream(inputPath, FileMode.Open), new FileStream(outputPath, FileMode.Create));
    }


The result of this HTML and iText code produces a PDF with device-dependent CMYK colors that can be used in a printing workflow, for example. 

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.