html: margins have no effect
The documentation and code suggest that the user can set page margins in the html method, but they have no effect. The margin options seem to get lost somewhere.
Some afterthoughts: The behavior between margins and the x/y and width (#2925) options is not immediately clear. I suggest the following:
- on the first page (the current page when
htmlis called), the startactualX(actualY) is the maximum of thex(y) value and theleft(top) margin - on subsequent pages, the starting
ycoordinate should be thetopmargin. Thexcoordinate should still be the max ofxandleftmargin - Similar to
x/y, the width option should be constrained by therightmargin: ifactualX + widthwould extend beyond the right margin, set the width topageWidth - actualX - right. So the formula would be:actualWidth = min(width, pageWidth - actualX - right) - The
bottommargin is simply the margin at the bottom of each page
.html is much better than addHtml, fromHtml, etc. I installed [email protected] for .html use. Please let me know why the margin is not working in .html. I want to use this method urgently.
I installed [email protected] for .html use. Please let me know why the margin is not working in .html. I want to use this method urgently.
Same problem. Margins in html method has no effect :( Also width and height of html2canvas options does not seam to make any difference eather... I'm using version 2.1.1.
Make sure you are avoiding collapasing margins. For example margin-bottom on one element, and margin-top on an element below it will collapse inward to the element NOT to the margins
@HackbrettXXX Yes I think that may be very useful, I currently have a letter generation use case and am dearly missing this feature.
Ok so has there been anyone who managed to solve this ? yes @HackbrettXXX my issue was a duplicate. I am setting margin with an array of random numbers but this has no effect on the printed pdf. I am using using the latest version of jsPdf and setting of margins does not work. v2.1.1 seems like it is very limited to customizability using the html() plugin compared to the previous versions of jsPdf using fromHtml() & addHtml() Do I have to install previous version below 2.0.0 such as 1.5.3 to achive the desired outcome ?
Please can someone guide me on this ? I desperately need tis margin feature !!
@kevinborg If you need the margins right now, you need to downgrade back to 1.5.3 and use from/addHTML.
If it can wait, you may wait for this issue to be resolved. You may also prepare a pull request yourself if it is urgent.
@HackbrettXXX I have downgraded to 1.5.3 but this causes a problem were the downloading a pdf does not work and i am unable to debug this because no console error is thrown. I have installed html2canvas separately and using addHTML method as shown below
const source = window.document.getElementById('invoice');
const pdf = new jsPDF('p', 'pt', 'a4');
const options = {
pagesplit: true
}
pdf.addHTML(source, 15, 25, options, () => {
pdf.save('file.pdf');
});
am I doing something wrong or missing something here ? Also do i need to install a specific version of html2canvas ?
I'd also like to use margins with the .html() method. It's fantastic, but when I have an element too tall to display on one page, the split happens exactly at the page edge -- not good for printing.
Are there plans to fix this or update the documentation?
We will fix this as soon as someone from the community provides a pull request. I personally don't have time for this currently.
Did anyone fixed or found a workaround for this problem?
I also really need this 'feature' to work properly. I think that 'upgrades' like this one, when a default behavior is getting broke, it should be a priority in getting it working. It shouldn't be an issue in the first place.
It's freaking frustrating when a library says it's doing something, and after you install it and use it, you realize that it's not actually doing what it's supposed to do...
@HackbrettXXX I would love to have a go at this, but I'm currently having trouble running the html function from the debugging page (from npm start).
It keeps giving this error:

@jeffsieu thanks. I'm assigning you.
In your debugging page, you need to load html2canvas (and dompurify if you pass a string to html). You can do so by adding a script tag, e.g.
<script src="node_modules/html2canvas/dist/html2canvas.js"></script>
@HackbrettXXX Is the content supposed to be scaled to fit the margins? And to be clear, is it context2d.js that manipulates the position/scale of the output?
@emildatcu I understand that this is frustrating, but please understand that this is a (free!) open source library that's currently maintained by a single person (me). And I simply don't have the time to fix everything immediately. Even keeping up with all new issues and pull requests is hard. That's why it's open source and everyone can contribute to this project :)
@jeffsieu The content should not be scaled to fit the margins. Instead, consider it as if the drawing canvas is the entire page minus the margins. If something would extend beyond the left/right margin, we should clip that. If something extends below the bottom margin, start a new page (with the top margin). See also my https://github.com/MrRio/jsPDF/issues/2924#issuecomment-698256602.
The position is AFAIK partly controlled by context2d.js and partly html.js. I'm not too familiar with both files.
@HackbrettXXX After a bit of digging around in the code, I still find it quite challenging to fully understand what's going on in the library.
To my understanding, the html function takes in an html element, then puts it in a pre-defined container, passes that container to html2canvas, then takes the resultant canvas and assigns it as a property of context2d, after which it gets turned into a pdf.
I'm still unsure at which point in the code does the canvas get split into pages, which is probably where the margin needs to be added. Can you provide any guidance?
The html module passes this.canvas to html2canvas, so all canvas API calls from html2canvas are consumed by the canvas.js module.
The splitting into pages is controlled by the autoPaging property in the context2d module. As far as I can see, the splitting happens in 3 functions: drawImage, pathPreProcess, and putText.