2012-02-17 83 views
80

我正在考慮使用pdf.js(允許在網頁中嵌入pdf的開源工具)。沒有關於如何使用它的任何文檔。如何使用pdf.js

我假設我所做的是在頭文件中引用的腳本創建一個html頁面,然後在正文中,我使用文件名和位置的數組進行某種函數調用。有人可以幫我從這裏出去嗎?

+0

### Github上文章我剛開始的一篇文章[在網站設置PDF.js(https://github.com/mozilla/pdf.js/wiki/Setup-PDF .js-in-a-website)在GitHub上的項目wiki上。 ###完成請求如果您有一些經驗,請完成文章。 – 2013-03-07 16:48:09

+6

我在找同樣的東西。我發現這是設置pdf.js最詳盡的步驟,從頭到尾。祝你好運! http://www.worldwidewhat.net/2011/08/render-pdf-files-with-html5/ – Raj 2012-04-12 04:06:56

+0

更像http://viewerjs.org/這樣的高層次可能是你想要的。 – max 2015-09-11 15:05:33

回答

32

嘗試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}); 

注:這裏所說的 「pdf.js」 項目是https://github.com/Marak/pdf.js,並已被廢棄了,因爲這個答案被張貼。 @ Treffynnon的答案是關於大多數搜索者都在尋找的仍然活躍的Mozilla項目(https://github.com/mozilla/pdf.js)。

+0

我看到了,但我對var = filename上面的東西感到困惑。我是否需要doc.addPage()中的任何一個到doc.text,然後在上面使用三個doc.texts? – Chris 2012-02-17 12:57:45

+0

另一個問題是我需要改變什麼。我假設我必須更改最後一行的第一個「文件名」,以及文檔屬性。是嗎? – Chris 2012-02-17 12:58:58

+19

這不是一個不同的pdf.js? – Swiss 2012-06-28 19:48:05

47

他們的github readme上有文檔可用。他們引用的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); 
    }); 
}); 
+14

它沒有很好的文檔記載,但是您解壓縮pdf.js zip並保持其目錄結構不變。然後,要查看pdf,只需導航到viewer.html文件(通過瀏覽器),並將文件追加到最後。 E.x. yoursite.com/directory_that_viewer_._html_is_in/viewer.html?file=somepdfthatyouhave.pdf pdf位置只是作爲GET變量傳遞給viewer.html文件。 – 2014-06-10 22:01:49

+1

從[github wiki](https://github.com/mozilla/pdf.js/wiki/Setup-PDF.js-in-a-website):*「但是,我們確實詢問您是否計劃嵌入在你自己的網站上查看它,它不僅僅是一個未修改的版本,請重新皮膚或建立它。「* - 鑑於他們可怕的不存在的[api文檔](http://mozilla.github.io/pdf .js/api /)這個項目確保你跳過足夠的箍來保持形狀:\ – Philzen 2016-12-05 23:11:38