javascript jsPDF 服务器端 (node.js) 使用 node-jspdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30694428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jsPDF server side (node.js) usage using node-jspdf
提问by user2736738
I am actually trying to include jspdf in server side and then use it for simple pdf generation(simply the text "Hello world!")(Go to the url- get the pdf localhost:8080). Now the first problem I face is
我实际上是在尝试将 jspdf 包含在服务器端,然后将其用于简单的 pdf 生成(只是文本“Hello world!”)(转到 url- 获取 pdf localhost:8080)。现在我面临的第一个问题是
- How to include it / What to do to use jsPDF in node?
- While trying to install it using
npm install node-jspdf
then it gives following error-
- 如何包含它/如何在节点中使用 jsPDF?
- 在尝试使用
npm install node-jspdf
然后安装它时它会出现以下错误 -
> G:\test\myproj>npm install node-jspdf
[email protected] 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! [email protected] install: `sh install.sh` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] 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
'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! [email protected] install: `sh install.sh` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] 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
Can anybody help me on how to include it in node? And give a 4-5 line demo.
有人可以帮助我如何将它包含在节点中吗?并给出4-5行demo。
回答by Simon Bengtsson
You can actually use jspdf directly (npm install jspdf
instead of npm install node-jspdf
). Jspdf is currently (v1.3.2) not built with node support in mind, but you can mock the globals like below and get it to work that way. This is a basic example and all features of jspdf will not be available.
您实际上可以直接使用 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;
回答by Bernhard
In extension to the answer provided by Simon Bengtsson:
在 Simon Bengtsson 提供的答案的扩展中:
I managed to handle even latin-1 characters by sending the output of jsPdf to encoding:
通过将 jsPdf 的输出发送到编码,我甚至设法处理了 latin-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;
回答by ShivaGanesh
I am able to install node-jspdfin Windows.
我可以在 Windows 中安装node-jspdf。
- As suggested download jspdf-master.zipand extract it.
Now use the command:
npm install <location on jspdf-master extract>
- 按照建议下载jspdf-master.zip并解压缩。
现在使用命令:
npm install <location on jspdf-master extract>
After that you can use npm list
to see the installation success.
之后就可以使用npm list
查看安装成功了。
回答by Tho
Node-jspdf
mainly uses library jsPDFas the core library to render PDF file
Node-jspdf
主要使用库jsPDF作为核心库来渲染PDF文件
but node-jspdf
can only be installed on *unix
system therefore you can download and install jsPDF
manually by following steps in this file
但node-jspdf
只能安装在*unix
系统上,因此您可以jsPDF
按照此文件中的步骤手动下载和安装
I think you only need to install jsPDF
to work with PDF file.
我认为您只需要安装jsPDF
即可使用 PDF 文件。
回答by Shikha
Download your jspdf folder from parallax jspdf. After installing jspdf ( "npm install jspdf" ) you get the jspdf folder inside node-modules. Go inside it and replace the jspdf.min.js by the jspdf.amd.min.js present in the /dist folder inside the jspdf downloaded from parallax
从视差 jspdf 下载您的 jspdf 文件夹。安装 jspdf ( "npm install jspdf" ) 后,您会在节点模块中获得 jspdf 文件夹。进入它并用从视差下载的 jspdf 内的 /dist 文件夹中的 jspdf.amd.min.js 替换 jspdf.min.js