Skip to content

Error: Unable to parse color from string: url(#pattern) when using method PDFPage.drawSvg #95

Description

@4ur3-zut

What were you trying to do?

Trying to draw an SVG element that contains a pattern definition and a polygon using it.

How did you attempt to do it?

Using PDFPage.drawSvg the same way you would with a solid color fill.

What actually happened?

Error: Unable to parse color from string: url(#pattern)

It tries to parse the reference to the pattern as a solid color.
The call to drawSvg fails and nothing is drawn.

What did you expect to happen?

Either draw the pattern correctly or recognize the feature as not implemented and finish drawing the polygon with a transparent fill.

How can we reproduce the issue?

I've included the files patternTest.js and patternTest.svg in a zip file.
You can run node patternTest.js to see the error and open patternTest.svg in a web browser to confirm what it looks like.

patternTest.js:

import { PDFDocument } from '@cantoo/pdf-lib';

const svg = `<svg width="1000" height="1000" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <pattern id="pattern" width="5" height="5" patternTransform="rotate(35) scale(4)" patternUnits="userSpaceOnUse">
            <rect width="5" height="5" style="stroke:none; fill: #f003"></rect>
            <rect width="2.5" height="5" style="stroke:none; fill: #f007"></rect>
        </pattern>
    </defs>
    <polygon points="0,0 0,100 100,100 100,0" fill="url(#pattern)" stroke="#000000" stroke-width="2.5" stroke-dasharray="6 3"></polygon>
</svg>`;

const document = await PDFDocument.create();

const page = document.addPage();

page.moveTo(10, 10);
page.drawSvg(svg); // <-- it will break here

Additionally this example won't trigger any errors with a solid color as fill value.
Note that my node is in module mode ("type": "module" in package.json).

patternTest.zip

Version

2.2.4

What environment are you running pdf-lib in?

Browser

Checklist

  • My report includes a Short, Self Contained, Correct (Compilable) Example.
  • I have attached all PDFs, images, and other files needed to run my SSCCE.

Additional Notes

My all in one example is in node, but my use case is in browser.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions