如何在使用Canvas元素时在PdfJs 3.4.120中显示标准工具栏?

huangapple go评论55阅读模式
英文:

How to show standard toolbar in PdfJs 3.4.120 while using Canvas element?

问题

以下是您要翻译的代码部分:

$.ajax({
    //some code
}).done(function(data, textStatus, jqXHR) {
    
    var pdfData = new Uint8Array(data);
    var pdfContainer = document.getElementById('pdfContainer');
    pdfjsLib.GlobalWorkerOptions.workerSrc = "pdf.worker.min.js";

    pdfjsLib.getDocument({ data: pdfData }).promise.then(function(pdf) {

        for(pageNum = 1; pageNum <= pdf.numPages; pageNum++) {

            pdf.getPage(pageNum).then(page => {

                const canvas = document.createElement('canvas');

                var scale = 1.5;
                var viewport = page.getViewport({ scale: scale, });

                var outputScale = window.devicePixelRatio || 1;
                var context = canvas.getContext('2d');

                canvas.width = Math.floor(viewport.width * outputScale);
                canvas height = Math.floor(viewport.height * outputScale);
                canvas.style.width = Math.floor(viewport.width) + "px";
                canvas.style.height = Math.floor(viewport.height) + "px";

                var transform = outputScale !== 1
                  ? [outputScale, 0, 0, outputScale, 0, 0]
                  : null;

                var renderContext = {
                  canvasContext: context,
                  transform: transform,
                  viewport: viewport
                };
                page.render(renderContext);
                pdfContainer.appendChild(canvas);
            });
        }
    });
})

<div id="pdfContainer"></div>

英文:

I'm trying to display a pdf file in my spring boot-MVC application's jsp page using Pdfjs 3.4.120 version.

When I use iFrame and viewer.html, a standard toolbar(example) gets displayed but as per my requirement I can't use iFrame. With canvas element I'm unable to see a toolbar and only pdf gets rendered.

I've gone through so many articles and posts but nothing helped.

following is my code:

$.ajax({
		//some code
	}).done(function(data, textStatus, jqXHR) {
	
		var pdfData = new Uint8Array(data);
		var pdfContainer = document.getElementById(&#39;pdfContainer&#39;);
		pdfjsLib.GlobalWorkerOptions.workerSrc = &quot;pdf.worker.min.js&quot;;

		pdfjsLib.getDocument({ data: pdfData }).promise.then(function(pdf) {

			for(pageNum = 1; pageNum &lt;= pdf.numPages; pageNum++) {

				pdf.getPage(pageNum).then(page =&gt; {

					const canvas = document.createElement(&#39;canvas&#39;);

					var scale = 1.5;
					var viewport = page.getViewport({ scale: scale, });

					var outputScale = window.devicePixelRatio || 1;
					var context = canvas.getContext(&#39;2d&#39;);

					canvas.width = Math.floor(viewport.width * outputScale);
					canvas.height = Math.floor(viewport.height * outputScale);
					canvas.style.width = Math.floor(viewport.width) + &quot;px&quot;;
					canvas.style.height =  Math.floor(viewport.height) + &quot;px&quot;;

					var transform = outputScale !== 1
					  ? [outputScale, 0, 0, outputScale, 0, 0]
					  : null;

					var renderContext = {
					  canvasContext: context,
					  transform: transform,
					  viewport: viewport
					};
					page.render(renderContext);
					pdfContainer.appendChild(canvas);

                            
				});
			}
		});
	})

&lt;div id=&quot;pdfContainer&quot;&gt;&lt;/div&gt;

答案1

得分: 1

PDF.js的主要目标是为Firefox浏览器提供PDF查看器,以查看本地PDF文件。目前,将其集成到项目中以具备自定义UI和功能并不容易。

pdfjs-dist包仅导出PDF.js的一些核心API,完全未导出查看器层API,因此无法从pdfjs-dist导入工具栏。

您可以构建自己的工具栏,拥有基本功能应该不难。如果您真的想使用默认工具栏,您需要自定义PDF.js的构建过程以导出工具栏,并使用与PDF.js相同的DOM结构,以完全相同的方式初始化工具栏。这是一项繁重的工作。

如果您只想显示带有默认工具栏的PDF,您可以尝试使用我维护的https://github.com/Priestch/document-viewer,它是用纯JavaScript编写的,目标是尽可能简化集成PDF查看器的过程。它具备PDF.js的所有功能。

英文:

The primary goal of PDF.js is to ship a PDF viewer for Firefox browser to view local PDF files, currently, it's not easy to integrate into a project to have custom UI and features.

The pdfjs-dist package only export some core API of PDF.js, all the viewer layer API are not exported totally, so you can't import the toolbar from pdfjs-dist.

You can build your own toolbar, it shouldn't be that hard to have basic functions. If you really want to use the default toolbar, you have to custom the build process of PDF.js to export the toolbar, use the same Dom structure, initialize the toolbar exactly the same way like PDF.js. It's a lot of work.

If you only want to show PDF with the default toolbar, you can try https://github.com/Priestch/document-viewer maintained by me, it's written in vanilla JavaScript, the goal is to make the process of integrating a PDF viewer as easy as possible. It has all the features of PDF.js.

huangapple
  • 本文由 发表于 2023年4月6日 23:14:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951116.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定