微信小程序pdf预览,且可以添加自定义水印(兼容安卓和ios)
uniapp微信小程序pdf预览
一、使用webview方式,访问pdf文件地点
- <web-view :src="showFilePath">
- </web-view>
- // showFilePath 是pdf文件访问路径
复制代码 但是这样有个题目,如果这个访问地点是使用了署理,不是直接访问pdf文件的路径(以pdf末端的路径),在小程序上安卓真机上会显示白屏,IOS可以显示。那么这个方式就不太可行
使用pdfjs实现预览pdf文件,支持自定义水印
我遇到的题目是:pdf文件在浏览器上访问是有水印的,但是通过pdfjs转成的预览,会直接忽略掉文档中的水印层,导致本来pdf文件有的水印,使用pdfjs渲染的内容没有水印,于是我就本身添加水印仅用于查看。以下是实现的代码:
- <template>
- <div class="pdf-container">
- <div class="image-container">
- <img v-for="(image, index) in pageImages"
- :key="index"
- :src="image"
- :alt="`Page ${index + 1}`"
- class="pdf-page-image">
- </div>
- </div>
- </template>
- <script setup>
- import { onMounted, ref } from 'vue';
- import pdfFile from './aa.pdf'
- import { useRoute } from 'vue-router';
- const route = useRoute();
- const pageImages = ref([]);
- const pdfUrl = ref('');
- const watermarkText = ref('');
- let pdfDoc = null;
- const waitForPdfJs = () => {
- return new Promise((resolve) => {
- const checkPdfJs = () => {
- if (window.pdfjsLib) {
- resolve();
- } else {
- setTimeout(checkPdfJs, 100);
- }
- };
- checkPdfJs();
- });
- };
- const convertPageToImage = async (pageNum) => {
- const page = await pdfDoc.getPage(pageNum);
- const viewport = page.getViewport({ scale: 2 });
-
- const canvas = document.createElement('canvas');
- const context = canvas.getContext('2d');
-
- canvas.height = viewport.height;
- canvas.width = viewport.width;
-
- await page.render({
- canvasContext: context,
- viewport: viewport
- }).promise;
-
- if (watermarkText.value) {
- context.save();
-
- context.globalAlpha = 0.2;
- context.font = '36px Arial';
- context.fillStyle = '#888';
-
- const text = watermarkText.value;
- const textWidth = context.measureText(text).width;
- const spacing = 130;
- const verticalSpacing = 100;
-
- for (let y = 50; y < canvas.height; y += verticalSpacing) {
- for (let x = -30; x < canvas.width; x += spacing + textWidth) {
- context.save();
- context.translate(x, y);
- context.rotate(-0.3);
- context.fillText(text, 0, 0);
- context.restore();
- }
- }
-
- context.restore();
- }
-
- return canvas.toDataURL('image/jpeg', 0.8);
- };
- const renderAllPages = async () => {
- const promises = [];
- for (let i = 1; i <= pdfDoc.numPages; i++) {
- promises.push(convertPageToImage(i));
- }
- pageImages.value = await Promise.all(promises);
- };
- onMounted(async () => {
- const encodedUrl = route.query.url || '';
- watermarkText.value = route.query.text ? decodeURIComponent(route.query.text) : ''; // 自定义水印的文字
-
- pdfUrl.value = encodedUrl ? decodeURIComponent(encodedUrl) : pdfFile; // pdfFile这个是本地pdf文件用来调试
- await waitForPdfJs();
-
- const loadingTask = pdfjsLib.getDocument(pdfUrl.value);
- loadingTask.promise.then(function (pdf) {
- pdfDoc = pdf;
- renderAllPages();
- });
- });
- </script>
- <style scoped>
- .pdf-container {
- display: flex;
- flex-direction: column;
- align-items: center;
- width: 100%;
- max-width: 1200px;
- margin: 0 auto;
- }
- .image-container {
- width: 100%;
- overflow-y: auto;
- background: #f5f5f5;
- box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
- }
- .pdf-page-image {
- display: block;
- margin: 0 auto 20px;
- width: 100%;
- max-width: 800px;
- height: auto;
- background: white;
- }
- </style>
复制代码 注意:
这里标注一下pdfjsLib这个对象是这两个文件里面有的,会把pdfjsLib放在window里面的
下面是效果哦!!!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |