如何使用 pdf.js

发布于 2025-01-07 07:40:47 字数 201 浏览 1 评论 0原文

我正在考虑使用 pdf.js (一种开源工具,允许在网页中嵌入 pdf )。没有任何关于如何使用它的文档。

我假设我所做的是使用标题中引用的脚本创建一个 html 页面,然后在正文中,我使用文件名和位置的数组进行某种函数调用。有人可以帮我吗?

I am considering using pdf.js (an open source tool that allows embedding of a pdf in a webpage). There isn't any documentation on how to use it.

I assume what I do is make an html page with the script referenced in the header, and then in the body, I put some sort of function call with an array of the file name and location. Can anyone help me out here?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎无痕… 2025-01-14 07:40:47

他们的 github 自述文件上提供了文档。 他们引用了以下示例代码

/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

//
// See README for overview
//

'use strict';

//
// Fetch the PDF document from the URL using promises
//
PDFJS.getDocument('helloworld.pdf').then(function(pdf) {
  // Using promise to fetch the page
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport(scale);

    //
    // Prepare canvas using PDF page dimensions
    //
    var canvas = document.getElementById('the-canvas');
    var context = canvas.getContext('2d');
    canvas.height = viewport.height;
    canvas.width = viewport.width;

    //
    // Render PDF page into canvas context
    //
    var renderContext = {
      canvasContext: context,
      viewport: viewport
    };
    page.render(renderContext);
  });
});

下面的代码对于 https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples

pdfjsLib.GlobalWorkerOptions.workerSrc = '/js/pdf.worker.js';

pdfjsLib.getDocument('helloworld.pdf')
    .promise
    .then(pdf => {
      pdf.getPage(1).then(page => {
        let outputScale = window.devicePixelRatio || 1;
        let transform = outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : null;
        let scale = 1.5;
        let viewport = page.getViewport({scale});

        let canvas = document.getElementById('the-canvas');
        let 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';

        let renderContext = {
          canvasContext: context,
          transform,
          viewport,
        };

        page.render(renderContext);
      });
    })
    .catch(console.error);

There is documentation available on their github readme. They cite the following example code:

/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

//
// See README for overview
//

'use strict';

//
// Fetch the PDF document from the URL using promises
//
PDFJS.getDocument('helloworld.pdf').then(function(pdf) {
  // Using promise to fetch the page
  pdf.getPage(1).then(function(page) {
    var scale = 1.5;
    var viewport = page.getViewport(scale);

    //
    // Prepare canvas using PDF page dimensions
    //
    var canvas = document.getElementById('the-canvas');
    var context = canvas.getContext('2d');
    canvas.height = viewport.height;
    canvas.width = viewport.width;

    //
    // Render PDF page into canvas context
    //
    var renderContext = {
      canvasContext: context,
      viewport: viewport
    };
    page.render(renderContext);
  });
});

Code below might be more accurate regarding https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples

pdfjsLib.GlobalWorkerOptions.workerSrc = '/js/pdf.worker.js';

pdfjsLib.getDocument('helloworld.pdf')
    .promise
    .then(pdf => {
      pdf.getPage(1).then(page => {
        let outputScale = window.devicePixelRatio || 1;
        let transform = outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : null;
        let scale = 1.5;
        let viewport = page.getViewport({scale});

        let canvas = document.getElementById('the-canvas');
        let 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';

        let renderContext = {
          canvasContext: context,
          transform,
          viewport,
        };

        page.render(renderContext);
      });
    })
    .catch(console.error);
愁杀 2025-01-14 07:40:47

尝试用 Google 搜索 pdf.js 文档

/* create the PDF document */

var doc = new pdf();
doc.text(20, 20, 'hello, I am PDF.');
doc.text(20, 30, 'i was created in the browser using javascript.');
doc.text(20, 40, 'i can also be created from node.js');

/* Optional - set properties on the document */
doc.setProperties({
  title: 'A sample document created by pdf.js',
  subject: 'PDFs are kinda cool, i guess',        
  author: 'Marak Squires',
  keywords: 'pdf.js, javascript, Marak, Marak Squires',
  creator: 'pdf.js'
});

doc.addPage();
doc.setFontSize(22);
doc.text(20, 20, 'This is a title');
doc.setFontSize(16); 
doc.text(20, 30, 'This is some normal sized text underneath.');

var fileName = "testFile"+new Date().getSeconds()+".pdf";
var pdfAsDataURI = doc.output('datauri', {"fileName":fileName});

注意:此处提到的“pdf.js”项目是 https://github.com/Marak/pdf.js,自发布此答案以来已被弃用。 @Treffynnon 的答案是关于仍然活跃的 Mozilla 项目 (https://github.com/mozilla/pdf.js )是大多数搜索者都会寻找的。

Try Google'ing pdf.js documentation

/* create the PDF document */

var doc = new pdf();
doc.text(20, 20, 'hello, I am PDF.');
doc.text(20, 30, 'i was created in the browser using javascript.');
doc.text(20, 40, 'i can also be created from node.js');

/* Optional - set properties on the document */
doc.setProperties({
  title: 'A sample document created by pdf.js',
  subject: 'PDFs are kinda cool, i guess',        
  author: 'Marak Squires',
  keywords: 'pdf.js, javascript, Marak, Marak Squires',
  creator: 'pdf.js'
});

doc.addPage();
doc.setFontSize(22);
doc.text(20, 20, 'This is a title');
doc.setFontSize(16); 
doc.text(20, 30, 'This is some normal sized text underneath.');

var fileName = "testFile"+new Date().getSeconds()+".pdf";
var pdfAsDataURI = doc.output('datauri', {"fileName":fileName});

NOTE: the "pdf.js" project mentioned here is https://github.com/Marak/pdf.js, and has been deprecated since this answer was posted. @Treffynnon's answer is about the still-active Mozilla project (https://github.com/mozilla/pdf.js) that most searchers will be looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文