加载中...

[Vue3]PDF的生成、预览与下载

预览
  1. const PDFPreview = (url) => {
  2. window.open(url,''_blank''); // 新窗口打开生成的图像
  3. }

下载
  1. const PDFDown = async (url) => {
  2. const response = await axios.get(url, { responseType: ''blob'' });
  3. const pdfUrl = window.URL.createObjectURL(new Blob([response.data]));
  4. const link = document.createElement(''a'');
  5. link.href = pdfUrl;
  6. link.setAttribute(''download'', ''专题图.pdf''); // 设置下载后保存的文件名
  7. document.body.appendChild(link);
  8. link.click();
  9. document.body.removeChild(link);
  10. }

生成

需求:截取某个div中的内容生成pdf文件,可自定义pdf大小,支持下载与预览

在div中添加 ref="thematicMap" ,定义 const thematicMap = ref(null)

pdf.js

  1. import html2canvas from "html2canvas";
  2. import jsPDF from "jspdf";
  3. export const downloadPDF = (page,type,value) => {
  4. return new Promise((resolve, reject) => {
  5. html2canvas(page, {
  6. useCORS: true,
  7. scale: 4,
  8. }).then(function (canvas) {
  9. let pdfBlob = canvas2PDF(canvas, type, value);
  10. resolve(pdfBlob);
  11. }).catch(function (error) {
  12. reject(error);
  13. });
  14. });
  15. };
  16. const canvas2PDF = (canvas,type,value) => {
  17. let contentWidth = canvas.width;
  18. let contentHeight = canvas.height;
  19. let imgWidth;
  20. let imgHeight;
  21. let direction;
  22. let paper;
  23. let title;
  24. //type 1a4横向 2a4纵向 3a3横向 4a3纵向
  25. //a4纸的尺寸210x297 a3 297x420
  26. if(type.paper===''自定义''){
  27. let height = contentHeight/297
  28. let width = contentWidth/420
  29. if(width<height){
  30. imgHeight = 297;
  31. imgWidth = 297/contentHeight * contentWidth;
  32. }else {
  33. imgWidth = 420;
  34. imgHeight = 420/contentWidth * contentHeight;
  35. }
  36. direction = "l"
  37. paper = ''a3''
  38. title = ''A3横向''
  39. }else if(type.paper===''A4横向''){
  40. imgHeight = 210;
  41. imgWidth = 297;
  42. direction = "l"
  43. paper = ''a4''
  44. title = ''A4横向''
  45. }else if(type.paper===''A4纵向''){
  46. imgWidth = 210;
  47. imgHeight = 297;
  48. direction = "p"
  49. paper = ''a4''
  50. title = ''A4纵向''
  51. }else if(type.paper===''A3横向''){
  52. imgHeight = 297;
  53. imgWidth = 420;
  54. direction = "l"
  55. paper = ''a3''
  56. title = ''A3横向''
  57. }else if(type.paper===''A3纵向''){
  58. imgWidth = 297;
  59. imgHeight = 420;
  60. direction = "p"
  61. paper = ''a3''
  62. title = ''A3纵向''
  63. }
  64. // 第一个参数: l:横向 p:纵向
  65. // 第二个参数:测量单位("pt""mm", "cm", "m", "in" or "px"
  66. let pdf = new jsPDF(direction, "mm",paper);
  67. pdf.addImage(
  68. canvas.toDataURL("image/jpeg", 1.0),
  69. "JPEG",
  70. 0,
  71. 0,
  72. imgWidth,
  73. imgHeight
  74. );
  75. if(value===''0''){
  76. //添加内容到 doc
  77. const blob = pdf.output(''blob'')
  78. //创建用于预览的 URL
  79. const url = URL.createObjectURL(blob)
  80. //显示 PDF 预览,例如在一个新窗口中
  81. window.open(url,''_blank''); // 新窗口打开生成的图像
  82. }else if(value===''1''){
  83. pdf.save("专题制图导出"+title+".pdf");
  84. }
  85. else if(value===''2''){
  86. // 将 pdf 数据转换为 Blob 对象
  87. const blob = pdf.output(''blob'')
  88. let pdfFile = new File([blob], "专题制图导出"+title+".pdf", { type: ''application/pdf'' });
  89. return pdfFile
  90. }
  91. };

vue组件中引用根据传入的dom生成pdf,根据传入的type决定是预览、下载还是返回pdf文件

预览

downloadPDF(thematicMap.value,ployForm.value,type)

获取pdf文件并上传

  1. downloadPDF(thematicMap.value,ployForm.value,type).then((a)=>{
  2. pdfFile = a
  3. //执行上传方法
  4. })