1. uniapp pdf链接预览
<template>
<VuePdfEmbed
:source="{
cMapUrl: cMapUrl,
url: pdfSource,
}"
/>
</template>
<script setup>
import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import VuePdfEmbed from "vue-pdf-embed";
const pdfSource = ref("/downloadobject.pdf");
const cMapUrl = ref("https://unpkg.com/pdfjs-dist/cmaps/");
onLoad((options) => {
pdfSource.value = options.pdfSource;
});
</script>
<style lang="scss">
.pdf-container {
height: 100vh;
}
</style>
2. html 页面下载为pdf文件
import html2canvas from 'html2canvas'
import { jsPDF } from 'jspdf'
export const downloadPDF = page => {
html2canvas(page, {
useCORS: true,
allowTaint: false,
scale: 2,
dpi: '192',
backgroundColor: '#ffffff'
}).then((canvas)=> {
canvas2PDF(canvas);
})
};
const canvas2PDF = canvas => {
const PDF = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4',
})
const ctx = canvas.getContext('2d')
const a4w = 190
const a4h = 277
const imgHeight = Math.floor(a4h * canvas.width / a4w)
let renderedHeight = 0
while (renderedHeight < canvas.height) {
let page = document.createElement("canvas");
page.width = canvas.width;
page.height = Math.min(imgHeight, canvas.height - renderedHeight);
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));
renderedHeight += imgHeight;
if (renderedHeight < canvas.height) {
PDF.addPage()
}
}
PDF.save("导出.pdf");
};
const downloadContract = () => {
downloadPDF(document.getElementById('container'))
}