aarelovich
aarelovich

Reputation: 5576

Using the path function of jsPDF

This a very simple example code that I wrote following the documentation of the path function for NodeJS. I basically want to draw a shape and have the border one color and be filled with anoter color. I thought path was the way to go. But it doesn't work:

//import { jsPDF } from "jspdf";
const { jsPDF } = require("jspdf"); // will automatically load the node version

// Default export is a4 paper, portrait, using millimeters for units
const doc = new jsPDF();

// Getting the page width and size 
var PAGE_WIDTH  = doc.internal.pageSize.getWidth();
var PAGE_HEIGHT = doc.internal.pageSize.getHeight();

doc.setDrawColor("#ff0000")
doc.setFillColor("#00ff00")

var h = PAGE_HEIGHT/2;
var w = PAGE_WIDTH/2;

var path = [];
path.push({op: "m", c: [0,0]})
path.push({op: "l", c: [w,0]})
path.push({op: "l", c: [w,h]})
path.push({op: "l", c: [0,h]})
path.push({op: "h", c: []})
doc.path(path);

doc.save("a4.pdf");

Can anyone suggest me why this doesn't draw anything?

Upvotes: 3

Views: 1047

Answers (1)

aarelovich
aarelovich

Reputation: 5576

So the code works, but it's incomplete. Much like the HTML canvas these fucntions (because path essentially calls moveTo, lineTo and close) don't do anything until you call stroke(), fill() or fillStroke().

In my case I was just missing

doc.fillStroke() 

after the doc.path statement

Upvotes: 2

Related Questions