这个问题通常是因为Firefox浏览器无法解析html2pdf的base64编码方式而导致的。解决方法是将编码方式改为Blob URL。
代码示例:
//导入html2pdf模块 import html2pdf from 'html2pdf.js';
//创建一个Blob URL function createBlobUrl(content: string): string { const blob = new Blob([content], { type: 'application/pdf' }); return URL.createObjectURL(blob); }
//使用Blob URL生成pdf generatePdf() { const element = document.getElementById('my-element'); const content = element.innerHTML;
const options = { filename: 'my-pdf-file.pdf', image: { type: 'jpeg' }, jsPDF: { orientation: 'landscape' } };
const url = createBlobUrl(content);
html2pdf().set(options).from(url).save(); }