Skip to content Skip to sidebar Skip to footer

How To Give Width, Height, X And Y Coordinates In Generating Pdf From Html Using Jspdf New Html Api

I have been using JSPDF to generate pdf document based on some html. Earlier using jspdf fromHTML Api, we could give margins like this var margins2 = { top: 415,

Solution 1:

If you look at the source code of jspdf.debug.js, html.js has the options for x and y offsets.

opt: {
    filename:'file.pdf',
    margin: [0, 0, 0, 0],
    enableLinks:true,
    x:0,
    y:0,
    html2canvas: {},
    jsPDF: {}
}

So you can set the x and y coordinates like this:

pdf.html(document.getElementById('html'), {
    x: 36,
    y: 36,
    callback: function () {
        // pdf.save('test.pdf');window.open(pdf.output('bloburl')); // to debug
    }
});

Unfortunately, I can't do the same by modifying the margin: [0, 0, 0, 0]. Looks like they are still working on this issue. So the short answer is NOT YET.

A work-around is to calculate the scale of html2canvas by margin:

let pdf = newjsPDF('p', 'pt', 'a4');
let margin = 36; // narrow margin - 12.7 mmlet srcwidth = document.getElementById('html').scrollWidth;
let scale = (595.28 - margin * 2) / srcwidth; // a4 pageSize 595.28

pdf.html(document.getElementById('html'), {
    backgroundColor: 'lightyellow',
    html2canvas: {
        scale: scale // default is window.devicePixelRatio,
    },
    x: margin,
    y: margin,
    callback: function () {
        window.open(pdf.output('bloburl'));
    }
});

Post a Comment for "How To Give Width, Height, X And Y Coordinates In Generating Pdf From Html Using Jspdf New Html Api"