PDF.JS库模糊的PDF模糊

发布于 2025-01-18 11:11:14 字数 2622 浏览 1 评论 0原文

我正在使用pdf.js库在画布上渲染我的pdf文件。 首先,我正在搜索一个解决方案,以渲染pdf的大小与帆布父元素的大小。 我找到了它,它可以正常工作。 然后,我解决了一次渲染所有页面的问题。 最后,我的代码现在看起来像:

pdfjsLib.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjsLib.version}/pdf.worker.min.js`;


class PdfLoader {
    currPage = 1;
    numPages = -1;
    doc = null;

    constructor($container) {
        this.$container = $container;
    }

    load(path) {
        // reset when using more than once
        this.currPage = 1;
        this.promise = new Promise(resolve => this.promiseResolve = resolve);
        this.$container.innerHTML = "";

        pdfjsLib.getDocument(path).promise.then(pdf => {
            this.doc = pdf;
            this.numPages = pdf.numPages;

            pdf.getPage(1).then(this._handlePage.bind(this));
        });

        return this;
    }

    _handlePage(page) {
        let viewport = page.getViewport({scale: 1});
        const scale = this.$container.clientWidth/viewport.width;
        // const outputScale = window.devicePixelRatio || 1;
        const outputScale = 1;

        viewport = page.getViewport({scale});

        const cnv = document.createElement("canvas");
        const ctx = cnv.getContext("2d");

        const width = Math.floor(viewport.width);
        const height = Math.floor(viewport.height);

        cnv.width = Math.floor(width * outputScale);
        cnv.height = Math.floor(height * outputScale);
        cnv.style.width = `${width}px`;
        cnv.style.height =  `${height}px`;

        const transform = (outputScale !== 1) ? [outputScale, 0, 0, outputScale, 0, 0] : null;

        page.render({
            canvasContext: ctx,
            transform: transform,
            viewport: viewport,
        });

        this.$container.appendChild(cnv);

        this.currPage++;
        if (this.doc !== null && this.currPage <= this.numPages) {
            this.doc.getPage(this.currPage).then(this._handlePage.bind(this));
        } else {
            this.promiseResolve();
        }
    }
}

const $pages = document.getElementById("pages");
const pdfLoader = new PdfLoader($pages);
pdfLoader.load("extendedReport.pdf").promise
    .then(initReport);


let arrow = null;

function initReport() {
  // my other stuff
}

现在我的问题是,在查看渲染的PDF时,它的质量看起来很低,并且文本模糊,因此在移动设备上无法阅读文档。我试图像他们在互联网上所说的那样改变量表,但事实并非如此。你能帮我吗,plz?我想念什么?

I'm using pdf.js library to render my pdf file on canvas.
First I was searching a solution for rendering pdf with the size of canvas parent element.
I've found it and it works fine.
Then I solve the problem of rendering ALL pages at once.
Finally, my code now looks this way:

pdfjsLib.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjsLib.version}/pdf.worker.min.js`;


class PdfLoader {
    currPage = 1;
    numPages = -1;
    doc = null;

    constructor($container) {
        this.$container = $container;
    }

    load(path) {
        // reset when using more than once
        this.currPage = 1;
        this.promise = new Promise(resolve => this.promiseResolve = resolve);
        this.$container.innerHTML = "";

        pdfjsLib.getDocument(path).promise.then(pdf => {
            this.doc = pdf;
            this.numPages = pdf.numPages;

            pdf.getPage(1).then(this._handlePage.bind(this));
        });

        return this;
    }

    _handlePage(page) {
        let viewport = page.getViewport({scale: 1});
        const scale = this.$container.clientWidth/viewport.width;
        // const outputScale = window.devicePixelRatio || 1;
        const outputScale = 1;

        viewport = page.getViewport({scale});

        const cnv = document.createElement("canvas");
        const ctx = cnv.getContext("2d");

        const width = Math.floor(viewport.width);
        const height = Math.floor(viewport.height);

        cnv.width = Math.floor(width * outputScale);
        cnv.height = Math.floor(height * outputScale);
        cnv.style.width = `${width}px`;
        cnv.style.height =  `${height}px`;

        const transform = (outputScale !== 1) ? [outputScale, 0, 0, outputScale, 0, 0] : null;

        page.render({
            canvasContext: ctx,
            transform: transform,
            viewport: viewport,
        });

        this.$container.appendChild(cnv);

        this.currPage++;
        if (this.doc !== null && this.currPage <= this.numPages) {
            this.doc.getPage(this.currPage).then(this._handlePage.bind(this));
        } else {
            this.promiseResolve();
        }
    }
}

const $pages = document.getElementById("pages");
const pdfLoader = new PdfLoader($pages);
pdfLoader.load("extendedReport.pdf").promise
    .then(initReport);


let arrow = null;

function initReport() {
  // my other stuff
}

And now my problem is that when viewing rendered pdf it looks like its quality is very low and text is blurred so the document is unreadable on mobile devices. I tried to change passed scale like they say on the internet, but that's not it. Could you help me, plz? What am I missing?

low quality of pdf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文