簡體   English   中英

使用node-jspdf的jsPDF服務器端(node.js)用法

[英]jsPDF server side (node.js) usage using node-jspdf

我實際上試圖在服務器端包含jspdf,然后將其用於簡單的pdf生成(簡單的文本“Hello world!”)(轉到url-獲取pdf localhost:8080)。 現在我面臨的第一個問題是

  • 如何包含它/如何在節點中使用jsPDF?
  • 在嘗試使用npm install node-jspdf進行安裝時,它會出現以下錯誤 -

> G:\test\myproj>npm install node-jspdf
 node-jspdf@0.0.3 install G:\test\myproj\node_modules\node-jspdf
 sh install.sh
 'sh' is not recognized as an internal or external command, operable program or batch file. npm ERR! Windows_NT 6.1.7601 npm ERR! argv "C:\\\\Program Files\\\\nodejs\\\\\\\\node.exe" "C:\\\\Program Files\\\\nodejs \\\\node_modules\\\\npm\\\\bin\\\\npm-cli.js" "install" "node-jspdf" npm ERR! node v0.12.4 npm ERR! npm v2.10.1 npm ERR! code ELIFECYCLE npm ERR! node-jspdf@0.0.3 install: `sh install.sh` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the node-jspdf@0.0.3 install script 'sh install.sh'. npm ERR! This is most likely a problem with the node-jspdf package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! sh install.sh npm ERR! You can get their info via: npm ERR! npm owner ls node-jspdf npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! G:\\test\\myproj\\npm-debug.log 

任何人都可以幫助我如何將其包含在節點中? 並提供4-5線演示。

您實際上可以直接使用jspdf( npm install jspdf而不是npm install node-jspdf )。 Jspdf目前(v1.3.2)沒有考慮到節點支持而構建,但你可以像下面那樣模擬全局變量並讓它以這種方式工作。 這是一個基本示例,jspdf的所有功能都不可用。

global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.html2pdf = {};
global.btoa = () => {};

var fs = require('fs');
var jsPDF = require('jspdf');

var doc = new jsPDF();
doc.text("Hello", 10, 10);
var data = doc.output();

fs.writeFileSync('./document.pdf', data, 'binary');

delete global.window;
delete global.html2pdf;
delete global.navigator;
delete global.btoa;

我可以在Windows中安裝node-jspdf

  1. 建議下載jspdf-master.zip並解壓縮。
  2. 現在使用命令:

     npm install <location on jspdf-master extract> 

之后,您可以使用npm list查看安裝成功。

延伸到Simon Bengtsson提供的答案:

我設法通過將jsPdf的輸出發送到編碼來處理甚至拉丁1字符:

global.window = {document: {createElementNS: () => {return {}} }};
global.navigator = {};
global.btoa = () => {};

var fs = require('fs');
var jsPDF = require('jspdf');
var encoding = require('encoding')
var doc = new jsPDF();
doc.text("HelloäöüßÄÖÜ©µ®", 10, 10);
var data = doc.output()
var buffer = encoding.convert(data, "Latin_1") 

fs.writeFileSync('./document.pdf', buffer);

delete global.window;
delete global.navigator;
delete global.btoa;

Node-jspdf主要使用庫jsPDF作為渲染PDF文件的核心庫

但是node-jspdf只能安裝在*unix系統上,因此您可以通過此文件中的以下步驟手動下載和安裝jsPDF

我認為您只需安裝jsPDF即可使用PDF文件。

從parallax jspdf下載jspdf文件夾。 安裝jspdf(“npm install jspdf”)后,您將獲得node-modules中的jspdf文件夾。 進入它並將jspdf.amd.js替換為jspdf.amd.min.js,它存在於從parallax下載的jspdf中的/ dist文件夾中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM