Skip to content

Set the margin to pdf page #2295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
suhasbhattu opened this issue Feb 26, 2019 · 25 comments
Closed

Set the margin to pdf page #2295

suhasbhattu opened this issue Feb 26, 2019 · 25 comments

Comments

@suhasbhattu
Copy link

suhasbhattu commented Feb 26, 2019

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?

@caliny97
Copy link

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.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Mar 23, 2019

Duplicate of #1176

@Uzlopak Uzlopak marked this as a duplicate of #1176 Mar 23, 2019
@Uzlopak Uzlopak closed this as completed Mar 23, 2019
@pedrovsp
Copy link

pedrovsp commented Jun 3, 2019

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.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Jun 3, 2019

@pedrovsp

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

@bluehazetech
Copy link

@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?

@jacekkoziol
Copy link

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.

@bytrangle
Copy link

bytrangle commented Aug 20, 2020

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?

@HackbrettXXX
Copy link
Collaborator

@bytrangle #2924.

@IgorKurkov
Copy link

IgorKurkov commented Dec 3, 2020

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

@CurryPaste
Copy link

CurryPaste commented Jan 22, 2021

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

@hotcakedev628
Copy link

@Uzlopak Any updates?

@Sun3
Copy link

Sun3 commented Feb 19, 2021

I am also looking for an update...

@Fernandogdo
Copy link

Hi, does anyone know how to fix this?

@HackbrettXXX
Copy link
Collaborator

@Fernandogdo see #2924.

@akomm
Copy link

akomm commented Nov 8, 2021

@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

@Uzlopak
Copy link
Collaborator

Uzlopak commented Nov 8, 2021

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 Uzlopak reopened this Nov 8, 2021
@HackbrettXXX
Copy link
Collaborator

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.

@sudolphiin
Copy link

sudolphiin commented May 31, 2022

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
...

@haidi20
Copy link

haidi20 commented Nov 1, 2022

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.

@rushi20139
Copy link

rushi20139 commented Apr 5, 2023

//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');
},
});

@clcoco
Copy link

clcoco commented Dec 7, 2023

//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'); }, });

It doesn't work properly, every new page has a different top margin. My guess is that it applies the bottom margin of the previous page

@hayyangovinda
Copy link

hayyangovinda commented Apr 18, 2024

Hello ,I think it is ok now, the method .html accepts margin as option ,pass it ,,then it should be fine :

image
table

@anuhyakalvakala
Copy link

Hello ,I think it is ok now, the method .html accepts margin as option ,pass it ,,then it should be fine :

image table

Thank you this is working. can we have any option where top margin and bottom margin are different something like [10,40,0]

@anuhyakalvakala
Copy link

Hello ,I think it is ok now, the method .html accepts margin as option ,pass it ,,then it should be fine :
image table

Thank you this is working. can we have any option where top margin and bottom margin are different something like [10,40,0]

worked with [top,left,bottom,right]

@harvey56
Copy link

You can get the page width, define a custom padding value :

const padding = 10;
const pageWidth = doc.internal.pageSize.getWidth();

Then, in each element you add to your doc, such as doc.text, you set its x as pageWidth - padding :

doc.text('my text', pageWidth - padding, my_Y, { align: "justify" });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests