Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  76] [ 5]  / answers: 1 / hits: 20497  / 8 Years ago, mon, june 6, 2016, 12:00:00

I'm currently using pdfmake to generate project report pdf's given information, and I'm having some trouble getting images to display.



I have a function that generates a pdfmake object that goes like this:



function singleProject(data) {
return {
text: Project: n + data.title + nnImage: n,
pageBreak: 'before'
}
}


I want to add an image to that report given an image URL (something like images/sample_image.jpg), and from what I've read on other answers I have to convert it to a base 64 format.



One of these functions was provided in another answer but I can't quite figure out how I'm supposed to be utilizing it:



function convertImgToBase64URL(url, callback, outputFormat){
var canvas = document.createElement('CANVAS'),
ctx = canvas.getContext('2d'),
img = new Image;
img.crossOrigin = 'Anonymous';
img.onload = function(){
var dataURL;
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);
dataURL = canvas.toDataURL(outputFormat);
callback(dataURL);
canvas = null;
};
img.src = url;
}


However, I'm not exactly too sure how I should go about using this function to add the image to the first function provided, as it doesn't return the dataURL; if I try something like:



function singleProject(data) {
return {
text: Project: n + data.title + nnImage: n,
image: convertImgToBase64URL(data.image), //data.image is the URL so something like images/sample_image.jpg
width: 300,
pageBreak: 'before'
}
}


The image doesn't show up.


More From » image

 Answers
70

Use hidden



<img id='imgToExport' src='someimageurl' style='display:none'/> 


and in JavaScript use



var imgToExport = document.getElementById('imgToExport');
var canvas = document.createElement('canvas');
canvas.width = imgToExport.width;
canvas.height = imgToExport.height;
canvas.getContext('2d').drawImage(imgToExport, 0, 0);
canvas.toDataURL('image/png')


By this way you donot need asynchronous call


[#61888] Friday, June 3, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
prestonh

Total Points: 384
Total Questions: 105
Total Answers: 105

Location: England
Member since Tue, Sep 8, 2020
4 Years ago
;