最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jsPDF working on first save, not updating on second - Stack Overflow

programmeradmin2浏览0评论

Scenario: the user picks a few choices, downloads a PDF. The user then changes a few choices, and downloads a new PDF.

Problem: everything works on the first run, but attempting to download a second PDF gives the user the same, old PDF.

My idea/guess: the function that runs the jsPDF actions (make doc, adding content, save doc) are called on click. My assumption would be that ALL actions get run anew on click, but that doesn't seem to be the case.

Is there some way to "clear" or "kill" the old PDF? I tried setting the var doc to null before running the function again, and that didn't help. Somehow the first PDF being generated is all I'm able to serve.

Here is the code that makes the PDF. Those frontImg and backImg vars contain the dataURI.

$(".button").click(function(){

  var doc = new jsPDF('landscape');

  doc.addImage(frontImg, 'PNG', 0, 0, 300, 210);
  doc.addPage('a6','l');
  doc.addImage(backImg, 'PNG', 0, 0, 300, 210);
  doc.save('file.pdf');

});

Anyone have any ideas here? Once the user takes actions that update the frontImg and backImg vars, shouldn't the PDF update too?

Scenario: the user picks a few choices, downloads a PDF. The user then changes a few choices, and downloads a new PDF.

Problem: everything works on the first run, but attempting to download a second PDF gives the user the same, old PDF.

My idea/guess: the function that runs the jsPDF actions (make doc, adding content, save doc) are called on click. My assumption would be that ALL actions get run anew on click, but that doesn't seem to be the case.

Is there some way to "clear" or "kill" the old PDF? I tried setting the var doc to null before running the function again, and that didn't help. Somehow the first PDF being generated is all I'm able to serve.

Here is the code that makes the PDF. Those frontImg and backImg vars contain the dataURI.

$(".button").click(function(){

  var doc = new jsPDF('landscape');

  doc.addImage(frontImg, 'PNG', 0, 0, 300, 210);
  doc.addPage('a6','l');
  doc.addImage(backImg, 'PNG', 0, 0, 300, 210);
  doc.save('file.pdf');

});

Anyone have any ideas here? Once the user takes actions that update the frontImg and backImg vars, shouldn't the PDF update too?

Share Improve this question asked Dec 19, 2014 at 20:56 clarkclark 1951 silver badge8 bronze badges 1
  • Hi, are you able to solve this issue? As I am facing the same issue too. Would appreciate it if you could update the answer :D – Stacie T. Commented Sep 13, 2021 at 7:09
Add a ment  | 

2 Answers 2

Reset to default 6

try to initialise the doc once again...

$(".button").click(function(){
    var doc = new jsPDF('landscape');

    doc.addImage(frontImg, 'PNG', 0, 0, 300, 210);
    doc.addPage('a6','l');
    doc.addImage(backImg, 'PNG', 0, 0, 300, 210);
    doc.save('file.pdf');
    doc = new jsPDF('landscape');
});

I had the same problem that the pdf was not updated when changing variables. Be careful and check if you are updating variables before creating a new pdf . on click you always create a new pdf .

发布评论

评论列表(0)

  1. 暂无评论