Reputation: 67
I need to insert an image into PDF , but I get error when the insert , tells me that is not formatted correctly . My code is as follows.
public getBase64Image(img : any) {
// Create an empty canvas element
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
// Copy the image contents to the canvas
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
// Get the data-URL formatted image
// Firefox supports PNG and JPEG. You could check img.src to
// guess the original format, but be aware the using "image/jpg"
// will re-encode the image.
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
the function that converts base64. which I call the function.
exporterPdfHeader:{
columns: [
{
width: 300, alignment: 'left',
text: [
'Pedido de prueba\n\n',
'Centro de Trabajo: prueba. Serie: 00.\n\n',
'Numped: 38289',
],
bold:true,
fontSize: 10,
margin: [40, 20, 0, 0],
height:120,
},
{
width: 140,
alignment: 'center',
image: 'data:image/png;base64, this.getBase64Image("../imagenes/logoExportarPdf.png")',
bold:true,
fontSize: 10,
margin: [40, 20, 0, 0],
height:80,
}
]
},
The error
On this, I have several questions. Do you need to pass it to base64 ?. All I wish is to insert a photo into a PDF , which is very simple. As a detail not know if it will be important, I use Angle ui -grid . I program in Typescript .
Thank you
Upvotes: 2
Views: 4632
Reputation: 1097
You're literally passing the string 'data:image/png;base64, this.getBase64Image("../imagenes/logoExportarPdf.png")' as image, not calling the function.
You should use 'data:image/png;base64,'+ this.getBase64Image("../imagenes/logoExportarPdf.png")
Upvotes: 3