foliojs / pdfkit

A JavaScript PDF generation library for Node and the browser
http://pdfkit.org/
MIT License
9.91k stars 1.15k forks source link

PDF Templating #227

Open brandondrew opened 10 years ago

brandondrew commented 10 years ago

Since many use cases (that I can think of, at least) for generating PDFs in the browser would involve either customizing something for a user (the PDF generated for Alice would often have a lot in common with the PDF generated for Bob and Carlos) or updating a PDF with new data (the PDF for today would be very similar to the PDF for yesterday) it would be extremely useful if pdfkit could load an existing PDF into memory to use as a template, and then add the data-driven bits to the template.

This could possibly improve generation performance, and would almost certainly improve developer productivity, allowing the base template to be manually created once, or generated in a tool that was already in use, and then customized for each user in the user's browser.

Not only could this make developers using pdfkit more productive, but it could ease the transition away from some server-side tool and to pdfkit.

cchantep commented 10 years ago

You can use Dhek to create a template for existing PDF file, as a separate JSON file which define areas (bounds, name, type, ...) to be filled/customized later for each user.

brandondrew commented 10 years ago

@cchantep How does Dhek integrate with PDFkit? It doesn't look like there's any good way: it appears to be a desktop app. (Unless you want to install Haskell on your server and only use PDFkit server-side, and write a lot of glue code to make PDFkit and Dhek work together.)

brandondrew commented 10 years ago

It does look like an interesting and useful app, though. I just don't see how it would give me templating in PDFkit.

cchantep commented 10 years ago

@brandondrew Dhek is agnostic about how the filling process is done, about which platform/language is used to read/write PDF (as JSON template format is open). It 'just' ease the pain of defining areas you want to write over.

It will mean a JS code will be able write over existing PDF using PDFkit, according the JSON template (which give information where to write which info, e.g. where on the PDF, 'name' provided by user should be overlayed/written).

zhouiscc commented 10 years ago

@cchantep does it mean we need to create the PDF by pdfkit first and override it by Dhek? an sample of it? node js one will be good.

cchantep commented 10 years ago

@zhouiscc You can use such template either to generate PDF from scratch, or to populate a base PDF with user data.

In first way, from scratch using PDFKit, you can design with Dhek two complementary templated: one for layout elements, other for user data elements. First defines area where to lay out elements common to all PDF (e.g. where to write document title, page description, ...), second defines where to fill user data (e.g. form inputs). From that your own code using PDFKit can iterates over templates & user data to generate each custom PDF from scratch.

For this first approach is a trick to define a 'layout' template: create an base PDF with blank pages (e.g. using LibreOffice PDF export). Indeed, as this template define layout areas, it should be define on an blank document, with as many blank pages as required.

Other approach is to create a template just from user data areas, base PDF being design in a graphical tool whatever is the one you prefer (e.g. Design your base document in LibreOffice and export it as PDF). In this way PDFKit can be used for now to 'write' user data according template areas on the existing/base document, but you can find web services able to (which are callable with some node.js code). Advantage is that it's much more easy to design layout (with preferred GUI tool).

zhouiscc commented 10 years ago

@cchantep thanks for the explanation. guess i have to figure out how to use Dhek. only few example available online.

cchantep commented 10 years ago

@zhouiscc Feel free to add a PR with a node.js

cchantep commented 10 years ago

@zhouiscc You might be interested in knowing that Dhek now allow to define area without existing PDF file: https://github.com/applicius/dhek/releases/tag/1.0.11

zhouiscc commented 10 years ago

@cchantep this looks interesting!! i will have a look. thanks.

aadamsx commented 9 years ago

@cchantep the Here is a Node.js code sample here: http://doc.applidok.com/merge-nodejs.html, is now a 404, is there a new place to find this example?

cchantep commented 9 years ago

See https://github.com/applicius/applidok-nodejs ( http://doc.applidok.com/integration.html )

aadamsx commented 9 years ago

I have the same problem, I'd like to use this tool to fill in PDF form data (inputs, etc.). Doesn't seem to, and doesn't look like it's on the roadmap. Alternatively, I'm looking into using pdftk command line tool that apparently does form fills.

brandondrew commented 9 years ago

I wish we could have this discussion somewhere else, since Dhek is interesting, but not really related to PDFkit, and actually not related to what is normally considered templating either. I'm not saying that to put Dhek down, but a complete templating solution would allow you to (for instance) use the table header template with data for the column names, and then use the row template for n rows of data, with pagination on every page and a page header and page footer on every page. Dhek seems to be specifically geared toward a mail merge sort of situation, where it is adding text to an existing PDF without any sort of control over layout of the resulting PDF.

If I'm mistaken, and Dhek has features I've missed that make it able to do full templating, I'd love to be corrected, but I still think it would be better for this discussion to happen elsewhere, away from the PDFkit issue log. If anyone has a good place for such a discussion, please post a link, and we can let this issue focus again on templating for PDFkit.

aadamsx commented 9 years ago

Right -- well, for people looking for a form fill solution (like you and me apparently), I thought I'd drop this comment in here to help someone is all (because I've been looking for a day or so for a solution myself). So, again, pdftk server: https://www.pdflabs.com/tools/pdftk-server/, not PDFkit, seems to do exactly what we want in this regard; maybe PDFkit for all else.

ChrisMulvay commented 9 years ago

Sorry if I missed something but is PDFtk only for Windows based systems?

aadamsx commented 9 years ago

Nope, read the website I posted above -- on all platforms.