jsPDF icon indicating copy to clipboard operation
jsPDF copied to clipboard

Set the margin to pdf page

Open suhasbhattu opened this issue 7 years ago • 25 comments

I am using jsPDF version 1.5.3 and I want to convert HTML into PDF. I also want to add margin to each pages of PDF so that content should not get cut while splitting it to next page. I have written following code until now:

var pdf = new jsPDF('p', 'pt', 'a4');

var margin = {top: 10, right: 20, bottom: 10, left: 20};

pdf.html(document.getElementById('content'), {
      callback : function (pdf) {
          pdf.save('content.pdf');
      }
});

I have a margin to set, but didn't find any way to set. Does anyone help me out here?

suhasbhattu avatar Feb 26 '19 11:02 suhasbhattu

I just spend the whole day searching how to apply margins to every page, with the "html" module and ran out of luck. Margins are such important elements when working with pdf. Any pointers are greatly appreciated.

caliny97 avatar Mar 14 '19 23:03 caliny97

Duplicate of #1176

Uzlopak avatar Mar 23 '19 17:03 Uzlopak

Should this issue really be marked as a duplicate? From I what I understood this issue regards the specific problem of setting margins to PDFs with the .html() method, whereas issue #1176 is a broad discussion on the new .html(). Also, issue #1176 doesn't seems to have solution nor discussion on this specific problem. Isn't it better for this issue to stay open until we find a solution and keep the other issue less polluted.

pedrovsp avatar Jun 03 '19 18:06 pedrovsp

@pedrovsp

Problem is, that all these things should be concentrated in one issue and not 100 issues of some of the same aspects.

Uzlopak avatar Jun 03 '19 18:06 Uzlopak

@pedrovsp

Problem is, that all these things should be concentrated in one issue and not 100 issues of some of the same aspects.

@Uzlopak I disagree with that logic. That keeps the number of defects down, but you'll end up with one gigantic defect with hundreds of issues, and fixing one is not tracked or documented anywhere in a clear way. It seems there are a bunch of issues closed as a duplicate of #1176 without any actual solutions provided.

Is there a solution to this margin issue specifically because I cannot find one anywhere?

bluehazetech avatar Oct 21 '19 16:10 bluehazetech

Hi guys, is there any solution available? I'm struggling with this problem, but I haven't found the way to solve it outside the library.

jacekkoziol avatar Mar 03 '20 10:03 jacekkoziol

So, if I understand it correctly, you are supposed to set margin by passing an array to the margin property.

doc.html(input, {
  margin: [left, top, right ,bottom] // the default is [0, 0, 0, 0]
  callback: myFunction(pdf) {
  }
}

But that doesn't work due to a bug in html2canvas, right?

bytrangle avatar Aug 20 '20 12:08 bytrangle

@bytrangle #2924.

HackbrettXXX avatar Sep 23 '20 16:09 HackbrettXXX

My best to set margin was

const pdf = new jsPdf({
        orientation: 'l',
        unit: 'pt',
        format: [canvas.width + 60, canvas.height + 160], // set surface larger according to desired margins
      });

      pdf.addImage(img, 'JPEG', 15, 40, canvas.width, canvas.height); // set margins there in image / 2 of margins above

hope it helps someone fix troubles quickly

IgorKurkov avatar Dec 03 '20 18:12 IgorKurkov

My best to set margin was

const pdf = new jsPdf({
        orientation: 'l',
        unit: 'pt',
        format: [canvas.width + 60, canvas.height + 160], // set surface larger according to desired margins
      });

      pdf.addImage(img, 'JPEG', 15, 40, canvas.width, canvas.height); // set margins there in image / 2 of margins above

hope it helps someone fix troubles quickly

If there is only one page, then this way is OK. However, if it is more than one page, there is no margin at the bottom of the first page and at the top of the second page. helpless😔 I mean the html method

CurryPaste avatar Jan 22 '21 09:01 CurryPaste

@Uzlopak Any updates?

hotcakedev628 avatar Feb 08 '21 10:02 hotcakedev628

I am also looking for an update...

Sun3 avatar Feb 19 '21 20:02 Sun3

Hi, does anyone know how to fix this?

Fernandogdo avatar Apr 08 '21 02:04 Fernandogdo

@Fernandogdo see #2924.

HackbrettXXX avatar Apr 08 '21 07:04 HackbrettXXX

@pedrovsp

Problem is, that all these things should be concentrated in one issue and not 100 issues of some of the same aspects.

maybe you should ask microsoft to remove the "new issue" button and instead everyone just posts directly. This way everything will be concentrated in one place instead of 100 different.

/sarcasm

akomm avatar Nov 08 '21 12:11 akomm

You should see this in the right context. When I wrote this, html plugin was relatively new. I was the only maintainer and had bigger plans for this plugin. So it was easier for me to concentrate all issues regarding .html into one issue to handle it in a central place.

2 years passed. I have no time to program in this project.

If your biggest concern is, that I closed it, then I just reopen it hereby.

Uzlopak avatar Nov 08 '21 14:11 Uzlopak

I think this issue should be closed. With [email protected] you can pass margins to the html function and they should work. There are also two different pagination modes now.

HackbrettXXX avatar Nov 10 '21 09:11 HackbrettXXX

Instead of setting the format on the original jsPDF object, set the format on the addPage method like so:

var doc = new jsPDF();
doc.addPage(canvas.width + margin, canvas.height + margin);
doc.addImage
...

sudolphiin avatar May 31 '22 14:05 sudolphiin

I am using jsPDF version 1.5.3 and I want to convert HTML into PDF. I also want to add margin to each pages of PDF so that content should not get cut while splitting it to next page. I have written following code until now:

var pdf = new jsPDF('p', 'pt', 'a4');

var margin = {top: 10, right: 20, bottom: 10, left: 20};

pdf.html(document.getElementById('content'), {
      callback : function (pdf) {
          pdf.save('content.pdf');
      }
});

I have a margin to set, but didn't find any way to set. Does anyone help me out here?

just set marign in css. it's can work.

haidi20 avatar Nov 01 '22 11:11 haidi20

//This seems to work for the most part :

let doc = new jsPDF('p', 'pt', 'a4'); let myImage = '../../../assets/logo.png';

doc.html(document.getElementById('htmlData'), { // (unknown, unknown, unknown, left) margin: [40, 60, 40, 60], callback: function (pdf) {

pdf.output('dataurlnewwindow'); }, });

rushi20139 avatar Apr 05 '23 07:04 rushi20139