Skip to content
New issue

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

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

Already on GitHub? # to your account

SVG patterns do not render when using minified version of pdfmake #1832

Closed
jpsilva opened this issue Oct 24, 2019 · 16 comments
Closed

SVG patterns do not render when using minified version of pdfmake #1832

jpsilva opened this issue Oct 24, 2019 · 16 comments

Comments

@jpsilva
Copy link

jpsilva commented Oct 24, 2019

Using patterns in SVG works as expected when importing the unminified version of pdfmake (pdfmake/build/pdfmake.js). But they render as solid black when either importing pdfmake.min.js or minimizing the original with Angular. I haven't noticed any other discrepancies between

Result with unminified version:
image

Result with minified version:
image

Code used to generate both results:

const pdfDoc = {
  content: [
    {
      svg: `
        <svg id="svg" viewBox="0 0 600 600">
          <defs>
            <pattern id="stripes" width="30" height="30" patternUnits="userSpaceOnUse">
              <path d="M0,0 15,0 0,15 z" fill="#00cc66" />
              <path d="M0,30 30,0 30,15 15,30 z" fill="#00cc66" />
            </pattern>
          </defs>
          <rect x="0" y="0" width="400" height="400" fill="#0044ff" />
          <rect x="200" y="200" width="400" height="400" fill="url(#stripes)" />
        </svg>
      `,
      width: 200
    }
  ]
};
const pdf = pdfMake.createPdf(pdfDoc);

pdf.open();
@liborm85 liborm85 added the bug label Dec 29, 2019
@liborm85 liborm85 added the svg label Mar 1, 2020
@matroskin
Copy link

@jpsilva managed to solve the problem?

@liborm85 liborm85 added the min label Aug 24, 2020
@hielfx
Copy link

hielfx commented Sep 25, 2020

I'm also facing this issue with v0.1.68

@ThePongBRIKL
Copy link

Hi, i also got this issue when working on client side. any update soon ?

@QuentinJanuel
Copy link

Hello, I have this issue as well.
Anyone knows how can I at least force the unminified version on ReactJS production mode?
I hope the bug will be fixed in a near future : )

@ThePongBRIKL
Copy link

Hi guy, good news, i just solve this issue !!.
So the issue it come from svg-to-pdfkit dependencies not from pdfmake.

The main issue is constructor of "PDFPattern()" turn it self to "Object" after build.
Then it make this condition away false "if (color[0].constructor.name === 'PDFPattern')"
About line 163 and 171
So that why it not assign your pattern.

How to fix it up to you for me i add more parameter to check it is pattern fill or not.
See my attach below.

If you dont know where then follow this suggestion

  • Copy my source.js
  • Then paste it to node_modules/svg-to-pdfkit/source.js
  • Then run build
  • That's it !!

my edit source.js
https://filebin.net/f6ja3bo4pmqqpglz/source.js?t=cnlux7mq

Screenshot - also work well with image pattern.
https://filebin.net/4838bdcuiee8eon1/Screenshot_2020-12-09_143730.jpg?t=lcnl1036

Sorry for my bad English.
Thank you, enjoy hacking everyone.

@liborm85
Copy link
Collaborator

Fixed by commit 27d4b1c (in 0.1 branch) and 8ce24e0 (in master)

@yagoar
Copy link

yagoar commented Feb 1, 2021

Hi, we're still getting this issue on version 0.1.70
After building the app for production with create-react-app, the SVG pattern is shown as a solid black box.

Development build result:
Bildschirmfoto 2021-02-01 um 12 03 20

Production build result:
Bildschirmfoto 2021-02-01 um 12 03 27

@rafaeelneto
Copy link

im getting the same issue with version 0.2.4

@liborm85
Copy link
Collaborator

Tested with 0.2.4 and works correctly.

@tahmidrahman-dsi
Copy link

tahmidrahman-dsi commented May 31, 2022

@liborm85 I am getting this error with version 0.2.5. Is there any fix or workaround?

@rafaeelneto
Copy link

after install the pdfmake you can change the pdfmake.js build file and change around the line 23032 that has

function docFillColor(color) {
    if (color[0].constructor.name === 'PDFPattern') {
      doc.fillOpacity(color[1]);
      docUsePattern(color[0], false);
    } else {
      doc.fillColor(color[0], color[1]);
    }
  }
function docStrokeColor(color) {
    if (color[0].constructor.name === 'PDFPattern') {
      doc.strokeOpacity(color[1]);
      docUsePattern(color[0], true);
    } else {
      doc.strokeColor(color[0], color[1]);
    }
  }

and add another condition to both if blocks

function docFillColor(color) {
    if (color[0].constructor.name === 'PDFPattern' || color[0].dx) {
      doc.fillOpacity(color[1]);
      docUsePattern(color[0], false);
    } else {
      doc.fillColor(color[0], color[1]);
    }
  }

  function docStrokeColor(color) {
    if (color[0].constructor.name === 'PDFPattern' || color[0].dx) {
      doc.strokeOpacity(color[1]);
      docUsePattern(color[0], true);
    } else {
      doc.strokeColor(color[0], color[1]);
    }
  }

if you're using something like github actions you can copy that modified pdfmake.js file into your project folder and run the following command after run npm install on cloud
npm ci && cp <your-pdfmake-folder>/pdfmakeCustom.js node_modules/pdfmake/build/pdfmake.js

@liborm85
Copy link
Collaborator

liborm85 commented Jun 7, 2022

Re-tested with 0.2.5 and works correctly.

@mickey58github
Copy link

mickey58github commented Aug 26, 2022

I get exactly the same problem when creating PDFs that contain pattern fills on Heroku Cloud with PDFsave. This is a big issue for our app. I'm using PDFSave v0.2.5. Is the above fix contained in v0.2.5 or not?

I tried the suggested workaround, overwriting the minified js file pdfmake.min.js with a custom version, but unfortunately that did NOT solve the issue. That leaves some doubts whether the suggested workaround really fixes the issue in all cases.

In my case, the issue shows with rectangles (rect tag) that use fill patterns through style = "fill: url(#my-pattern-id)". Those render in black color using the production build.

@MoosArga
Copy link

I get the same issue with 0.2.7. The fix on svg-to-pdfkit suggested by ThePongBRIKL solves the issue.
Any other option to avoid that kind of solutions ? I'm not a big fan to have to change my dependencies sources to get things work.

rafaeelneto added a commit to rafaeelneto/pdfmake that referenced this issue Feb 10, 2024
@LeGoffMael
Copy link

LeGoffMael commented Feb 13, 2024

I also encountered this issue today while using the latest version 0.2.9.
I am using Vite in my project and the issue seems the be coming from vite's build minification.

I fixed it by adding those lines to the vite.config.ts (which basically reproduce 8ce24e0) :

build: {
    minify: 'terser',
    terserOptions: {
      keep_classnames: true,
      keep_fnames: true
    }
  },

another solution is simply to disable the minification

build: { minify: false },

@kfmadhusudhanj
Copy link

As @rafaeelneto mentioned in his comment we can do same code changes

function docFillColor(color) {
    if (color[0].constructor.name === 'PDFPattern' || color[0].dx) {
      doc.fillOpacity(color[1]);
      docUsePattern(color[0], false);
    } else {
      doc.fillColor(color[0], color[1]);
    }
  }

  function docStrokeColor(color) {
    if (color[0].constructor.name === 'PDFPattern' || color[0].dx) {
      doc.strokeOpacity(color[1]);
      docUsePattern(color[0], true);
    } else {
      doc.strokeColor(color[0], color[1]);
    }
  }

To execute this we can use npm hooks in package.json

 "scripts": {
    "postinstall": "cp <your-pdfmake-folder>/pdfmakeCustom.js node_modules/pdfmake/build/pdfmake.js 
 }

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests