Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
jsPDF setPage no funciona con set html

He creado elementos div que contienen html de cada página PDF. Intento generar PDF usando jsPDF. El problema es que coloca todas las páginas html en la primera página del archivo PDF.

He probado este código

 const list = []; const pages = document.getElementsByName( this.pageName ); const pdf = new jsPDF("landscape", "px", [this.pageWidth, this.pageHeight]); pages.forEach((el, i) => { if (i > 0) { pdf.addPage(); } pdf.setPage(i + 1); list.push(pdf.html(el)); }); Promise.all(list).then((res) => { pdf.save("test.pdf"); }).catch(error => { console.log('error ', error); });

He probado este código

 const list = []; const pages = document.getElementsByName( this.pageName ); const pdf = new jsPDF("landscape", "px", [this.pageWidth, this.pageHeight]); pages.forEach((el, i) => { if (i > 0) { pdf.addPage(); } list.push(pdf.html(el, {x: 0, y: (i * this.pageHeight)})); }); Promise.all(list).then((res) => { pdf.save("test.pdf"); }).catch(error => { console.log('error ', error); });

addPage funciona bien, genera páginas en blanco, pero aún coloca todas las páginas html en la primera página del archivo PDF. Entonces, de alguna manera pdf.setPage(i + 1); y las soluciones {x: 0, y: (i * this.pageHeight)} no funcionan.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Finalmente pude obligar a jsPDF a colocar contenido HTML en las páginas correspondientes. Aquí está la solución.

 ... const list = []; const pages = document.getElementsByName( this.pageName ); const pdf = new jsPDF("landscape", "px", [this.pageWidth, this.pageHeight]); this.setOnePage(pdf, 0, pages, list); ... private setOnePage(pdf: jsPDF, i: number, pages: NodeListOf<HTMLElement>, list: Array<Promise<any>>): void { if (i < pages.length) { if (i > 0) { pdf.addPage(); } list.push(pdf.html(pages.item(i), {x: 0, y: (i * this.pageHeight)}).then((e) => { this.setOnePage(pdf, ++i, pages, list); })); } else { Promise.all(list).then((res) => { pdf.save("test.pdf"); }).catch(error => { console.log('error ', error); }); } }

La solución parece un poco complicada, por lo que si alguien conoce una mejor solución, de nada.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!