parallax / jsPDF

Client-side JavaScript PDF generation for everyone.

Home Page:https://parall.ax/products/jspdf

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Impossible to add image in NodeJS

ir-fuel opened this issue · comments

I have read and understood the contribution guidelines.

I am using jsPDF 2.5.1 in Nodejs 18, and trying to add an image to my pdf. I have tried the following ways:

  • using a base64 data uri
  • using an Uint8Array

both of the above methods I have tried with a png file with transparency, a png file without, and a jpg file. The result is either garbage (for the png with transparency), nothing at all, or a black rectangle.

This is the code:

const { jsPDF: JSPDF } = require('jspdf')
const fs = require('fs')

require('jspdf-autotable')

const number = 1
const logo = fs.readFileSync(`${__dirname}/logo_plain.png`)
const doc = new JSPDF()
doc.addImage(logo, 180, 10, 20, 20)

const buffer = doc.output('arraybuffer', {
    filename: `report-${number}.pdf`
  })

fs.writeFileSync(`report-${number}.pdf`, buffer)

The result can be seen in the attached file. I also added the png file I've used to generate this pdf.

report.pdf

logo_plain

The same library is used in a client-side web app, and there adding the image as follows works without issues:

  doc.addImage('/logo_plain.png', 'png', 180, 10, 20, 20)

Nothing has changed here and it started working all of the sudden ... it also worked first try in our server env. The issue was only on my mac.