一、需求:
頁面上某一部分內容需要生成pdf并下載
二、技術方案:
使用html2canvas和jsPDF插件
?
三、js代碼
// 頁面導出為pdf格式
import html2Canvas from "html2canvas";
import jsPDF from "jspdf";
import { uploadImg } from '@/api/public';const htmlToPdf = {getPdf(title, id) {html2Canvas(document.querySelector(id), {allowTaint: false,taintTest: false,logging: false,useCORS: true,dpi: window.devicePixelRatio * 4, //將分辨率提高到特定的DPI 提高四倍scale: 4, //按比例增加分辨率}).then((canvas) => {var pdf = new jsPDF("p", "mm", "a4"); //A4紙,縱向var ctx = canvas.getContext("2d"),a4w = 190,a4h = 272, //A4大小,210mm x 297mm,四邊各保留10mm的邊距,顯示區域190x277imgHeight = Math.floor((a4h * canvas.width) / a4w), //按A4顯示比例換算一頁圖像的像素高度renderedHeight = 0;while (renderedHeight < canvas.height) {var page = document.createElement("canvas");page.width = canvas.width;page.height = Math.min(imgHeight, canvas.height - renderedHeight); //可能內容不足一頁//用getImageData剪裁指定區域,并畫到前面創建的canvas對象中page.getContext("2d").putImageData(ctx.getImageData(0,renderedHeight,canvas.width,Math.min(imgHeight, canvas.height - renderedHeight)),0,0);pdf.addImage(page.toDataURL("image/jpeg", 1.0),"JPEG",10,10,a4w,Math.min(a4h, (a4w * page.height) / page.width)); //添加圖像到頁面,保留10mm邊距renderedHeight += imgHeight;if (renderedHeight < canvas.height) {pdf.addPage(); //如果后面還有內容,添加一個空頁}// delete page;}console.log(pdf)// pdf.save(title + ".pdf"); // PDF.save() 此方法可以將pdf直接保存本地let pdfData = pdf.output('datauristring') // 獲取base64Pdflet files = dataURLtoFile(pdfData, '封面.pdf') // 將base64文件轉化為流,上傳ossuploadFiles(files)});},
};function dataURLtoFile(dataurl, filename) {let arr = dataurl.split(',')let mime = arr[0].match(/:(.*?);/)[1]let bstr = atob(arr[1])let n = bstr.lengthlet u8arr = new Uint8Array(n)while (n--) {u8arr[n] = bstr.charCodeAt(n)}return new File([u8arr], filename, { type: mime })
}function uploadFiles(files) {let data = new FormData()data.append('file', files)uploadImg(data).then(res => {console.log(res)if (res.data.code == 0) {// formData.value.qrcode = res.data.data.res.url}})
}export default htmlToPdf;
四、使用步驟:
1. 使用之前需要先按照2個npm包
npm install html2canvas -S --registry=https://registry.npmmirror.com/
npm install jspdf -S --registry=https://registry.npmmirror.com/
2. 在頁面引用
import htmlToPdf from "@/utils/htmlToPDF";
3. 調用方法
const printReport = () => {
? ? htmlToPdf.getPdf('測試pdf',"#db-test-report");
}
4. 記得給指定容器加id
五:注意事項:
1. 如果要打印的內容是iframe里面的,因為無法直接拿到dom元素,需要跟iframe通訊,拿到dom元素,放到當前頁面;如果樣式沒帶過來,特殊處理一下
const iframe = document.getElementById("myIframe");
// 向iframe發送消息
iframe.contentWindow.postMessage("getElement", tooltipUrl.value);// 收到iframe的回復才能生成pdf
window.addEventListener("message", function (e) {console.log("iframe回復的消息");console.log(e);if (e.origin === iframeOrigin.value && e.data.type === "element") {// 處理獲取到的元素var container = document.getElementById("pdf-iframe");container.innerHTML = e.data.html;hasIframePdf = truegetPdf("xxx下載", "#pdf-xxx-xxx");}
});