40

I need to change default pdf page width and font size in jspdf.debug.js.

Where and how to change the default values in jspdf.debug.js?

user3428816
  • 1,011
  • 3
  • 15
  • 30

4 Answers4

80

Besides using one of the default formats you can specify any size you want in the unit you specify.

For example:

// Document of 210mm wide and 297mm high
new jsPDF('p', 'mm', [297, 210]);
// Document of 297mm wide and 210mm high
new jsPDF('l', 'mm', [297, 210]);
// Document of 5 inch width and 3 inch high
new jsPDF('l', 'in', [3, 5]);

The 3rd parameter of the constructor can take an array of the dimensions. However they do not correspond to width and height, instead they are long side and short side (or flipped around).

Your 1st parameter (landscape or portrait) determines what becomes the width and the height.

In the sourcecode on GitHub you can see the supported units (relative proportions to pt), and you can also see the default page formats (with their sizes in pt).

Aidiakapi
  • 5,590
  • 3
  • 30
  • 58
  • if i want to change the length of one page only is this possible –  Jul 29 '14 at 13:06
  • I'm not 100% sure it's not possible (maybe there's a plugin for this), but skimming through the source code made it seem pretty clear it's not in the default implementation. – Aidiakapi Jul 29 '14 at 21:07
  • Could you look at this [Question](http://stackoverflow.com/questions/25001939/not-getting-desired-output-in-jspdf) –  Jul 30 '14 at 05:50
  • 1
    Yes, you can. Check my answer on: https://stackoverflow.com/questions/41848540/jspdf-change-page-format/46504177#46504177 – zameb Sep 30 '17 at 15:51
  • Neat, but back in the day, `addPage` did not yet take arguments: [old](https://github.com/MrRio/jsPDF/blob/ddbfc0f0250ca908f8061a72fa057116b7613e78/jspdf.js#L612), [new](https://github.com/MrRio/jsPDF/blob/master/jspdf.js#L762). – Aidiakapi Sep 30 '17 at 23:07
32

From the documentation page

To set the page type pass the value in constructor

jsPDF(orientation, unit, format) Creates new jsPDF document object

instance Parameters:

orientation One of "portrait" or "landscape" (or shortcuts "p" (Default), "l")

unit Measurement unit to be used when coordinates are specified. One of "pt" (points), "mm" (Default), "cm", "in"

format One of 'a3', 'a4' (Default),'a5' ,'letter' ,'legal'

To set font size

setFontSize(size)

Sets font size for upcoming text elements.

Parameters:

{Number} size Font size in points.

Community
  • 1
  • 1
Ramesh
  • 12,212
  • 2
  • 48
  • 83
4

For anyone trying to this in react. There is a slight difference.

// Document of 8.5 inch width and 11 inch high
new jsPDF('p', 'in', [612, 792]);

or

// Document of 8.5 inch width and 11 inch high
new jsPDF({
        orientation: 'p', 
        unit: 'in', 
        format: [612, 792]
});

When i tried the @Aidiakapi solution the pages were tiny. For a difference size take size in inches * 72 to get the dimensions you need. For example, i wanted 8.5 so 8.5 * 72 = 612. This is for jspdf@1.5.3.

levif1
  • 45
  • 1
  • 8
2

My case was to print horizontal (landscape) summary section - so:

}).then((canvas) => {
  const img = canvas.toDataURL('image/jpg');
  new jsPDF({
        orientation: 'l', // landscape
        unit: 'pt', // points, pixels won't work properly
        format: [canvas.width, canvas.height] // set needed dimensions for any element
  });
  pdf.addImage(img, 'JPEG', 0, 0, canvas.width, canvas.height);
  pdf.save('your-filename.pdf');
});
Kurkov Igor
  • 1,474
  • 13
  • 19