Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when trying to create a dotted line #1060

Open
florian-escola opened this issue Nov 27, 2019 · 7 comments
Open

Error when trying to create a dotted line #1060

florian-escola opened this issue Nov 27, 2019 · 7 comments

Comments

@florian-escola
Copy link

Bug Report

Description of the problem

I get an Error: dash([0,2], {}) invalid, lengths must be numeric and greater than zero Error when tying to create a dotted line unsing .lineCap('round').dash(0, { space: 2 }).

From some research I found this was introduced because of #949 in #969. This bit of the PDF spec was mentioned there:

The dash array’s elements are numbers that specify the lengths of alternating dashes and gaps; the numbers must be nonnegative and not all zero.

The way I interpret that is that the length and the space can't both be zero.

So [0 2] 0 d would be valid whereas [0] 0 d or [0 0] 0 d would be invalid.

As far as I understand it, this is the only way to create a dotted line.

Workaround

Do it manually:

doc
    .moveTo(0, 10)
    .lineTo(100, 10)
    .lineCap('round')
    .addContent('[0 2] 0 d')
    .stroke();

Code sample

const PDFDocument = require('pdfkit');
const fs = require('fs');

const doc = new PDFDocument;

doc.pipe(fs.createWriteStream('output.pdf'));

doc
    .moveTo(0, 10)
    .lineTo(100, 10)
    .lineCap('round')
    .dash(0, { space: 2 })
    .stroke();

doc.end();

Your environment

  • pdfkit version: 0.10.0
  • Node version: 12.13.1
  • Browser version (if applicable):
  • Operating System: Ubuntu
@blikblum
Copy link
Member

@floyd-may , can you take a look at it?

@PascalPixel
Copy link

Downgrading to 0.9.1 will fix this, which is before they added this error.
The error notes it was added because some viewers can't view dash with values of 0, so I guess I'm exposing to risk, but I truly need a 0 in there. 🤷🏻‍♂️

@blikblum
Copy link
Member

@PascalPixel do mind creating a PR with an unit test (basically the code from OP)?

@floyd-may
Copy link
Contributor

@blikblum sorry I didn't see this earlier! I switched jobs and my subscription to this repo was pointed at my work email address =(

I agree that this appears to be over-zealous validation. I was able to use this code to produce a dotted line that renders correctly in MS Edge:

doc
    .moveTo(0, 10)
    .lineTo(100, 10)
    .lineCap('round')
    .addContent('[0 2] 0 d')
    .stroke();

I don't know that I have the bandwidth to make a PR for at least a week, but if this is still unaddressed by that time, please ping me here and I'll try to carve out a couple hours.

@PascalPixel
Copy link

Google Chrome renders without issues as well 🎉

Referring to your PR @floyd-may #969, do you remember which renderers specifically had an issue?

@floyd-may
Copy link
Contributor

@PascalPixel it was MS Edge and Adobe Reader that had issues.

@PascalPixel
Copy link

Ok Adobe Reader is pretty important 😅

bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this issue Apr 1, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this issue Apr 1, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this issue Apr 1, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this issue Apr 1, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
bendemboski added a commit to chadkirby/SVG-to-PDFKit that referenced this issue Apr 4, 2022
In PDFKit >=0.10.0, calling dash() with any 0s in the dash array/length will throw an error. For more info see

foliojs/pdfkit#1060
foliojs/pdfkit#949
foliojs/pdfkit#969

A number of SVG editors, I believe including inkscape, will happily generate `stroke-dasharray` attributes with 0s in them, and then running them through SVG-to-PDFKit will cause the conversion to fail completely. So, this commit does a bunch of math to rewrite dash arrays containing 0s to eliminate the zeros, but still achieve the same result.

This commit also updates the version of PDFKit the examples use to the latest (0.13.0) and adds a test that exercises a bunch of `stroke-dasharray` scenarios with 0s in them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants