79 lines
2.3 KiB
JavaScript
79 lines
2.3 KiB
JavaScript
// import jsPDF from 'jspdf';
|
||
// import html2canvas from 'html2canvas';
|
||
|
||
|
||
// export function useExportToPDF() {
|
||
// const exportToImage = async (element, filename = 'screenshot.png') => {
|
||
// const canvas = await html2canvas(element, {
|
||
// scale: 2,
|
||
// useCORS: true,
|
||
// });
|
||
|
||
// // 转换为图片并下载
|
||
// const link = document.createElement('a');
|
||
// link.download = filename;
|
||
// link.href = canvas.toDataURL('image/png');
|
||
// link.click();
|
||
// };
|
||
|
||
// // 将div导出为PDF的方法
|
||
// const exportDivToPDF = async (element, filename = 'document.pdf') => {
|
||
// try {
|
||
// // 使用html2canvas将div转换为canvas
|
||
// const canvas = await html2canvas(element, {
|
||
// scale: 2, // 提高清晰度
|
||
// useCORS: true, // 允许跨域图片
|
||
// logging: false, // 关闭日志
|
||
// width: element.offsetWidth,
|
||
// height: element.offsetHeight,
|
||
// windowWidth: element.scrollWidth,
|
||
// windowHeight: element.scrollHeight,
|
||
// backgroundColor: '#ffffff' // 设置背景色为白色
|
||
// });
|
||
|
||
// // 获取canvas的宽高
|
||
// const imgWidth = 210; // A4纸宽度(mm)
|
||
// const pageHeight = 297; // A4纸高度(mm)
|
||
// const imgHeight = canvas.height * imgWidth / canvas.width;
|
||
// let heightLeft = imgHeight;
|
||
// let position = 0;
|
||
|
||
// // 创建PDF文档
|
||
// const pdf = new jsPDF({
|
||
// orientation: 'portrait',
|
||
// unit: 'mm',
|
||
// format: 'a4'
|
||
// });
|
||
|
||
// // 将canvas转换为图片并添加到PDF
|
||
// const imgData = canvas.toDataURL('image/png');
|
||
// pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
|
||
// heightLeft -= pageHeight;
|
||
|
||
// // 如果内容超出一页,添加新页面
|
||
// while (heightLeft > 0) {
|
||
// position = heightLeft - imgHeight;
|
||
// pdf.addPage();
|
||
// pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
|
||
// heightLeft -= pageHeight;
|
||
// }
|
||
|
||
// // 下载PDF
|
||
// pdf.save(filename);
|
||
// } catch (error) {
|
||
// console.error('导出PDF失败:', error);
|
||
// alert('导出PDF失败,请稍后重试');
|
||
// }
|
||
// };
|
||
|
||
// return {
|
||
// exportToImage,
|
||
// exportDivToPDF
|
||
// };
|
||
// }
|
||
// exportToImage
|
||
// };
|
||
// }
|
||
export function exportDivToPDF(divId, filename) {
|
||
}
|