最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - Generate PDF from Template using Javascript - Stack Overflow

programmeradmin10浏览0评论

The situation is the following: We have a website, that generates (large) contract-JSONs which we now want to export as pdfs using the frontend, so javascript. At the moment, I use pdfmake but i find it very hard to get a layout that stays the same for every contract. I would prefer to have some kind of template which i could fill with the values from the JSON.

Best I can think of at the moment would be to write a pdf template with LaTex and fill it with some template framework and pile it with a LaTex piler. However I hope there is something simpler you could tell me. It's only important, to get from JSON to the pdf in one click.

The situation is the following: We have a website, that generates (large) contract-JSONs which we now want to export as pdfs using the frontend, so javascript. At the moment, I use pdfmake but i find it very hard to get a layout that stays the same for every contract. I would prefer to have some kind of template which i could fill with the values from the JSON.

Best I can think of at the moment would be to write a pdf template with LaTex and fill it with some template framework and pile it with a LaTex piler. However I hope there is something simpler you could tell me. It's only important, to get from JSON to the pdf in one click.

Share Improve this question asked May 13, 2015 at 10:41 Urr4Urr4 7931 gold badge10 silver badges29 bronze badges 3
  • So far my proposted 'best I can think of' did not work since I hoped I could avoid forcing our customers to pile java by using a LaTeX Compiler javascript library. However, there seems to be only one: texlive.js, which does not seem to work offline (what i want, sadly). So I will continue using pdfmake. Looking forward to ideas to make this simpler (or at least more readable) – Urr4 Commented May 13, 2015 at 13:53
  • Have you considered to use client-side javascript libraries like jsPDF.js to generate the whole PDF client-side? – Eugene Commented May 13, 2015 at 17:43
  • I did, but I used pdfmake instead of jsPDF, because I've read a few times that it allows better formatting. – Urr4 Commented May 15, 2015 at 5:43
Add a ment  | 

3 Answers 3

Reset to default 3

I have done some research for a similar scenario. Below are three options that i shortlisted :

  1. HTML-PDF (nodejs module)
  2. WKTHMLTOPDF
  3. PhantomJS (even the first two options uses phantomjs)
  4. jsPDF - can be used at client side

After having this solution running for some time, I want to add that changing the document afterwards (add a table for example) is pretty tough in this scenario. If I'd have to do it again, I'd probably try to implement the building blocks of the pdf as latex-templates with placeholders. Like directives in angular. In the document building process I would go through template by template, generate the Latex-Blocks (like tables) using the data I received from the backend and I'd use FreeMarker to fill the generated data into the templates and afterwards put the templates into one plete latex file, holding the whole pdf in latex. Using a js-Latex piler like texlive, I should be able to generate the pdf from this file.

All of this is in theory and probably more work than what I did, but I like this solution better, since it's cleaner and easier to maintain. Learn from my mistakes :-)

I ended up, using pdfmake. First I understood it wrong, which was the cause for me not being able to edit a pdf. Now I know, that you can layout your document nicely by using lots of columns.

I tried to read a .tex file and fill it using FreeMarker. This worked very well, however not having an online LaTeX piler, i would have had to force customers to pile the .tex files locally which i did not want.

However, if you have a LaTeX piler locally, this should work.

发布评论

评论列表(0)

  1. 暂无评论