Insert Firestore Uploaded Image In Jspdf
I have an uploaded image in Firestore and I am able to get download URL in my Angular application. Download URL is like: https://firebasestorage.googleapis.com/v0/b/appname.appspo
Solution 1:
According to the jsPDF documentation the addImage
method accepts its imageData
in these formats:
imageData
:string
|HTMLImageElement
|HTMLCanvasElement
|Uint8Array
imageData as base64 encoded DataUrl or Image-HTMLElement or Canvas-HTMLElement
So you can't simply pass in a URL that points to an image.
A simple way to get the image into a support format is by creating a img
element in code:
var img = document.createElement('img');
img.src = this.downloadURL;
doc.addImage(img, 'JPEG', 30, 20);
Solution 2:
I get this working doing the following steps:
First, follow this solution Set CORS in your firebase Storage
I used the following cors.json
[{"origin":["*"],"method":["GET"],"maxAgeSeconds":3600}]
Then just use the following code to read your url and add it to your pdf:
asyncgeneratePDF(your_image_url: string) {
letimageLoaded: HTMLImageElement = awaitthis.getDataUri(your_image_url);
let pdf = newjsPDF('p', 'pt', 'a4');
pdf.addImage(imageLoaded, 0, 0, 250, 250);
pdf.save('test.pdf');
}
asyncgetDataUri(url): Promise<HTMLImageElement> {
returnnewPromise(resolve => {
var image = newImage();
image.onload = () => {
resolve(image);
};
image.src = url;
})
}
Hope this helps you, I was searching alot for this solution, so shared as much as you can.
Regards.
Post a Comment for "Insert Firestore Uploaded Image In Jspdf"