I am facing an issue while trying to generate PDF file using LWC component. I fetch data from a HTTP callout and try to generate a PDF file out of the response and display using LWC. What can be the root cause of this error message?
Error Message:
Error Failed to load PDF document.
URL displayed in the new window address bar: blob:https://mybox.sandbox.lightning.force.com/3dfde707-8bb4-4d55-b36d-25dccabdf7d9
import { LightningElement, api, wire } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
export default class Z_PDFViewer extends NavigationMixin(LightningElement) {
@api recordId;
pdfBlobUrl;
@wire(generatePDF, { recordId: '$recordId' })
wiredPDF({ error, data }) {
if (data) {
console.log('PDF Content:', data);
const blob = new Blob([data], { type: 'application/pdf' });
this.pdfBlobUrl = URL.createObjectURL(blob);
} else if (error) {
console.error('Error generating PDF:', error);
}
}
handleClick() {
//window.open(this.pdfBlobUrl, '_blank');
console.log('Blob URL:', this.pdfBlobUrl);
this[NavigationMixin.Navigate]({
type: 'standard__webPage',
attributes: {
url: this.pdfBlobUrl
},
});
}
}
HTML:
<template>
<div>
<a onclick={handleClick} href="javascript:void(0);">Open PDF in New Window</a>
</div>
</template>