javascript javascript中的HTML图像到pdf转换器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24908067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:33:22  来源:igfitidea点击:

HTML image to pdf convertor in javascript

javascriptjqueryhtmlpdf-generationjspdf

提问by Bad Coder

HTML CODE:

HTML代码:

<img class="imagepdf "src="www.images.com/someimage" alt="Smiley face" height="42" width="42">

I want to convert this image to pdf format. I am using jspdf()for this but i am not getting my output ?Can someone give me live example on fiddle for pdf conversion for chrome.

我想将此图像转换为 pdf 格式。我为此使用了jspdf(),但我没有得到我的输出?有人可以给我关于 chrome pdf 转换的小提琴示例。

回答by

Here is code from it's github site

这是来自它的 github 站点的代码

var getImageFromUrl = function(url, callback) {
    var img = new Image();

    img.onError = function() {
        alert('Cannot load image: "'+url+'"');
    };
    img.onload = function() {
        callback(img);
    };
    img.src = url;
}


var createPDF = function(imgData) {
    var doc = new jsPDF();



    doc.addImage(imgData, 'JPEG', 10, 10, 50, 50, 'monkey');
    doc.addImage('monkey', 70, 10, 100, 120); // use the cached 'monkey' image, JPEG is optional regardless



    doc.output('datauri');
}

getImageFromUrl('thinking-monkey.jpg', createPDF);

If you are working in chrome security restrictions that prevent it from loading images from a url so just add this line and it will be working fine
img.crossOrigin = " ";
It will be fine for chrome alse

如果您在 chrome 安全限制中工作,阻止它从 url 加载图像,那么只需添加这一行,它就会正常工作
img.crossOrigin = " ";
铬也可以