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

javascript - Trying to add image to pdf using jsPDF - Stack Overflow

programmeradmin4浏览0评论

Trying this code

pdfGenerate.js

generatePDF = function() {
var imgData = 'D:/work/TiffImages/png/895153.0000.png';
var doc = new jsPDF('p', 'pt', 'a4');
doc.text(20, 20, 'CTS Aarchival');
doc.addImage(imgData, 'PNG', 15, 40, 180, 180);
doc.save('CTStest.pdf');    }

Error:

Uncaught TypeError: doc.addImage is not a function

In HTML I am just calling this method onclick() And all required js files are included.

Trying this code

pdfGenerate.js

generatePDF = function() {
var imgData = 'D:/work/TiffImages/png/895153.0000.png';
var doc = new jsPDF('p', 'pt', 'a4');
doc.text(20, 20, 'CTS Aarchival');
doc.addImage(imgData, 'PNG', 15, 40, 180, 180);
doc.save('CTStest.pdf');    }

Error:

Uncaught TypeError: doc.addImage is not a function

In HTML I am just calling this method onclick() And all required js files are included.

Share Improve this question asked Nov 29, 2018 at 14:08 Adarsh SinghAdarsh Singh 2081 gold badge3 silver badges17 bronze badges 1
  • Problems with addImage. Which file are you importing? – reisdev Commented Nov 29, 2018 at 14:17
Add a ment  | 

3 Answers 3

Reset to default 5

Simple solution:

Instead of using jsPDF.js library use jsPDF.debug.js , it includes all the modules which we need.

addImage function is in another module called *drumroll please* addImage. So if you're importing the jsPdf.js it doens't contain that module.

Here is the doc link. Also check out these github issues here and here

You can also do it in this way:

var pdf = new jsPDF();
var img = new Image;
img.onload = function() {
    pdf.addImage(this, 10, 10);
    pdf.save("CTStest.pdf");
    };
img.crossOrigin = "";  

img.src = 'D:/work/TiffImages/png/895153.0000.png';
发布评论

评论列表(0)

  1. 暂无评论