<p>Hello, i'd like to ask if someone knows any library that uses some sort of templateing for generating PDFs. Right now i'm using <a href="https://github.com/unidoc/unidoc" rel="nofollow">https://github.com/unidoc/unidoc</a> for creating some PDFs, which works ok, but is very verbose, as everything needs to be done in the code (colors/paragraphs, etc.). I also tried <a href="https://github.com/jung-kurt/gofpdf" rel="nofollow">https://github.com/jung-kurt/gofpdf</a> but thats the same.</p>
<p>I have some data (lists, images/bar charts) which need to be printed in a PDF. The PDFs are about 5-15 pages big and most data is layouted as tables.
I'm looking for something like the ReportManager from VisualStudio, where you can create the report independently from the data in a template and then just feed in the data.</p>
<p>Would be cool to have this as a go-lib but an external application would also be ok (should run on windows and linux). Can be free or chargeable.</p>
<p>Any tips are very welcome.
Thanks</p>
<hr/>**评论:**<br/><br/>LadyDascalie: <pre><p>The way I've solved a similar problem is by making html templates that render the content I want, then printing them to PDF using <a href="https://github.com/SebastiaanKlippert/go-wkhtmltopdf" rel="nofollow">SebastiaanKlippert/go-wkhtmltopdf</a></p>
<p>This makes the actual PDF generation code quite small, and it lets you work with regular html templates and css for crafting your document, which in my opinion is much easier (and in my case, let our front end devs take care of all the CSS stuff).</p></pre>renemarxis: <pre><p>This looks great. As far as i saw i should be able to
1. Generate the template to a buffer
2. Use that buffer as stdin for wkhtmltopdf
3. Let wkhtml2pdf save the pdf to file
4. Serve the pdf</p>
<p>What i like here is, that wkhtmltopdf does not need to make a https request to the application, so no authentication etc needs to be done. Also the application (more specific the it infrastructure) does not allow to make https calls from inside the server to the server (big company with big restrictions)</p></pre>LadyDascalie: <pre><p>Correct.</p>
<p>I just keep the binary of wkhtmltopdf in a <code>bin</code> folder, and call it on the fly when necessary, all communication is strictly between this binary and the application itself, nothing over the wire.</p>
<p>What I do personally I generate a temporary, final HTML file, and use that as input, because in my case, I also use the produced HTML for emailing purposes.</p>
<p>I think you've figured out the same basic strategy that I went for!</p>
<p>Good luck with this, if you run into issues, shoot me a message and I'll be happy to share whatever knowledge I may have!</p></pre>arachnist: <pre><p>I can recommend <a href="http://prawnpdf.org/" rel="nofollow">http://prawnpdf.org/</a></p>
<p>It's ruby, but there's no better alternative for it.</p></pre>printf_hello_world: <pre><p>Just to summarize some of the other comments: Don't use actual PDFs as templates, use some intermediary instead. <em>(HTML seems like a reasonable choice)</em></p>
<p>I recently spent 4 months writing a layer on top of a PDF rendering library in order to extract and recycle all of the data within (with enough fidelity that you really could use a PDF as a template). It was a monumental effort, and that was just for the bare-bones feature set.</p>
<p>The reality is that nobody except Adobe fully supports the 1.7 spec with all extensions. Foxit, iText and pdfbox are fairly close, but they are neither Go, nor easy to use, nor free (with the exception of pdfbox). *Oh, and pdfium (a fork of Foxit maintained by Chrome) is free, but extremely difficult to use.</p>
<p>Depending on your licensing requirements, PoDoFo and Poppler (both open-source) could also be used to read and understand template PDFs, but their feature set is more restricted.</p>
<p>Plus, I'm advising you not to use template PDFs, so you should probably ignore all the stuff about library support other than to know that it's daunting.</p></pre>nicochamp: <pre><p>I would suggest checking <a href="http://weasyprint.org" rel="nofollow">http://weasyprint.org</a>. I used it to generate nice looking multi-pages invoices and it works beautifully. You can create your reports using Go's template package, and then use weasyprint to convert it to PDF. </p></pre>Tikiatua: <pre><p>Hi there,</p>
<p>We use PrinceXML as external utility to generate PDFs from a html source <a href="https://www.princexml.com" rel="nofollow">https://www.princexml.com</a>. This works really well and the PrinceXML system has some great additions especially in respect to hyphenation and block text layout. It can also render svg and execute javascript when creating the pdf. So it is really easy to create awesome reports with charts using a javascript library.</p>
<p>Unfortunately it is rather expensive. But depending on your budget, it might be a very good option (it was also used by Google for quite some time). We have ben using it in production for about 2.5 years and had no problems at all.</p>
<p>Another free option would be <a href="https://wkhtmltopdf.org" rel="nofollow">https://wkhtmltopdf.org</a>, which is open source. We are currently looking into this, but did not yet have the time to properly evaluate the capabilities.</p></pre>the_duck_life: <pre><p>Has Prince's accuracy improved lately? I recall testing it out like 6 years ago and HTML looked not at all accurate. I eventually fell on EvoPDF to go from HTML to PDF and have had nearly no issues ever with it looking bad.</p></pre>fmontag451: <pre><blockquote>
<p>external application would also be ok</p>
</blockquote>
<p>I tend avoid direct pdf generation from my code since it's usually a pain. What about generating something in one of the input formats supported by <a href="https://pandoc.org/" rel="nofollow">pandoc</a>? You could write your data into a markdown file (or LaTeX if you need fancy stuff) and then convert everything to pdf. The bonuses would be:</p>
<ul>
<li>you need to consider just one, easily writable format</li>
<li>this <em>intermediate</em> format is human-readable-and-writeable </li>
<li>via pandoc you can generate a lot of other stuff other than pdf</li>
</ul></pre>renemarxis: <pre><p>Thank you all for the responses. I will have a close look to all hints( ͡° ͜ʖ ͡°)</p></pre>dim13: <pre><p>IMHO, of all pdf rendering possibilities, only LaTeX does its job well. As starting point I would suggest you my small <a href="https://hub.docker.com/r/dim13/gotex/" rel="nofollow">latex-to-pdf µService</a>.</p></pre>