如果想实现将后端二进制转pdf请看我另一篇文章
VUE3中使用PDF.js实现二进制转PDF预览功能_pdfjs 二进制-CSDN博客
代码如下:
javascript
<template>
<img :src="imageUrl" style=" width: 267.3mm; max-height: 189mm" />
</template>
const imageUrl = ref();
imageUrl.value = await convertPdfPageToImageUrl(data);
async function convertPdfPageToImageUrl(pdfData) {
GlobalWorkerOptions.workerSrc = "/public/static/pdf/build/pdf.worker.mjs";
const loadingTask = await getDocument({ data: pdfData });
const pdfDocument = await loadingTask.promise;
const pageNumber = 1;
const pdfPage = await pdfDocument.getPage(pageNumber);
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const viewport = pdfPage.getViewport({ scale: 1.0 });
canvas.width = viewport.width;
canvas.height = viewport.height;
await pdfPage.render({
canvasContext: context!,
viewport: viewport
}).promise;
return canvas.toDataURL("image/png");
}