pdfkit中的HTML表(Expressjs-Nodejs) [英] HTML table in pdfkit (Expressjs-Nodejs)

查看:259
本文介绍了pdfkit中的HTML表(Expressjs-Nodejs)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pdfkit生成pdf文件,我想把这个pdf文件发送给浏览器。


我的下面的代码工作正常,我得到一个pdf文本。


其实下面的代码是在nodejs中使用pdfkit生成pdf的例子,但现在我想创建html表。



最新代码



  var PDFDocument = require('pdfkit'); 
var fs = require('fs');
doc = new PDFDocument();
doc.pipe(fs.createWriteStream('out.pdf'));
doc.moveTo(300,75)
.lineTo(373,301)
.lineTo(181,161)
.lineTo(419,161)
。 lineTo(227,301)
.fill('red','even-odd');

var loremIpsum ='Lorem ipsum dolor sit amet,consectetur adipiscing elit。 Etiam在...'

doc.y = 320;
doc.fillColor('black')
doc.text(loremIpsum,{
paragraphGap:10,
indent:20,
align:'justify',
列:2
});
doc.pipe(res);
doc.end();

但是我不知道如何使用pdfkit在pdf中生成HTML表格?


可以帮助我创建HTML表格pdf吗?

解决方案

不用直接用PDFKit做。你必须自己实现表格呈现逻辑。如果你想简单地做,你只需要意识到,表只是一堆矩形,其中包含文本。这将适用于一次性代码。如果你不介意偏离PDFKit一点点,那么有几个选择:





看到你提到HTML,我真的建议你把它放在门外HTML并使用 phantomjs wkhtmltopdf ,他们的工作是渲染HTML和可选地输出PDF,这就是你想要的。上一次我正在寻找一个可以处理这个问题的模块,我发现 phantom-html-to -pdf


I am using pdfkit to generate pdf file and i want to send this pdf file to browser.
My following code is working fine and i am getting one pdf with text.
Actually following code is sample to generate pdf using pdfkit in nodejs but now i want to create html table.

Latest Code

var PDFDocument = require('pdfkit');
var fs=require('fs');
doc = new PDFDocument();
doc.pipe( fs.createWriteStream('out.pdf') );
doc.moveTo(300, 75)
   .lineTo(373, 301)
   .lineTo(181, 161)
   .lineTo(419, 161)
   .lineTo(227, 301)
   .fill('red', 'even-odd');  

var loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in...';  

doc.y = 320;
doc.fillColor('black')
doc.text(loremIpsum, {
   paragraphGap: 10,
   indent: 20,
   align: 'justify',
   columns: 2
});
doc.pipe( res );
doc.end();

But i don't have any idea how to generate HTML table in pdf using pdfkit?
Can any one help me to create HTML table pdf?

解决方案

Well there's no easy to do it directly with PDFKit. You have to implement the table rendering logic yourself. If you wanna do it simply, you just have to realize that tables are just a bunch of rectangles with text into them. This will work with a one-off code. It won't be flexible though.

If you don't mind deviating from PDFKit a little bit, there's a couple of options:

  • There's a fork of PDFKit which has tables support. Here's an example of it should be used.
  • There's pdfmake which is built on top of PDFKit and supports tables. Pdfmake supports a declarative syntax unlike PDFKit.

And seeing you mention HTML, I would really suggest throw PDFkit out of the door when you have HTML and use phantomjs or wkhtmltopdf, which their job is to render HTML and optionally output PDF and that's what you want. Last time I was looking for a module that handles this well, I found phantom-html-to-pdf.

这篇关于pdfkit中的HTML表(Expressjs-Nodejs)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆