Best Way to Generate PDF with Dynamic Templates Data

Use Case: I want to generate PDF reports with dynamic Data values
I wonder how you guys are doing the PDF thing in the production application (Meteor 3)

assume that The above method is a perfect fit for my needs but sadly both packages are deprecated (phantomJS and web shot already archived) and meteorhacks:ssr No longer works with Meteor 3

What I tried:

  1. I tried to create PDF with puppeteer (works)

  2. I tried to compile a dynamic template with ssr like this below source (works) (([2.1.1] SpacebarsCompiler is not defined · Issue #11387 · meteor/meteor · GitHub)

  3. I tried to compile a dynamic template + data with SSR (Failed) error image below

Although Puppeteer PDF works fine the deployment process on Galaxy seems strange too, so I also want to know, how you all generate PDFs for Meteor Applications

you can find a minimal demo here

here is the deployed version of the demo (but you know it generates PDF for the first 2 methods after 20-60 seconds)
https://test-pdf.meteorapp.com/

Now I’m using pupeeteer. Before I was using wkhtmltopdf. HTML template I have stored in mongo (not files like in your case) and I have method ‘replace’ which will replace {{variable}} into variable value. I’m not using SSR and Blaze for that (less meteor packages, less ‘headache’, especially in migration).

Hmm interesting, thanks for opening the new door, however, I wonder in that case how you compile styles (CSS) and also if we have more {{variables}} as in my use-case I may have almost 30 variable values in some user cases, it would be good if you provide minimal sample code if possible (only minimal part so I got an idea)
anyways thanks for sharing your experience I will test it with different combinations

You might wanna look into this:

About 20 years or more ago I created a ColdFusion project based in the iText library to create fully personalized (text + images) books with that. So you can do a lot with iText.

I haven’t used Flying Saucer yet, but it’s open source and based on iText. So it might be worth looking into this.

You can generate your reports as documents in DB and call a render to a page (a route in your project) receiving those ids as params (E.g. www.example.com/rep=report_id.
What prerender (the render endpoint) does is to render the HTML of you report.
Pupeteer uses a headless browser to render your page and you can return HTML, PDF, JPG, PNG etc.

Ideally, you would have this service as a third party, either a dedicated server or a serverless function.

I am checking different ways to finalize a way, that satisfies my use cases however I didn’t understand why there is no alternative to meteorhacks/ssr after investigation I see there are only one or two Blaze/SpaceBar core methods not exported publicly, Otherwise it’s the best option to compile template with dynamic data for many use cases (email/pdf/partials, etc) I see there is an open issue about meteor 3 support but of course that package already dead


I will dig more now with Meteor’s internal code so if I can compile a dynamic template with data somehow else will go this route, will share my findings lately
Thanks

Puppeeter is starting browser on the background, therefore inline or header CSS is not a problem. In my case, I have stored html and css inside mongo (document template) and then I will read the collection, replace the replacements {{variable}} and also put css into header.

And then I will generate pdf from prepared html. Only what was the problem was incompatibility of latest puppeeter package with the meteor 2 (old node), which was solved with meteor 3. Now it is working without any problems.

About the replacements: I will dynamically prepare objects to replace (user, quotation, …), then I have defined list of accepted fields and then fields (in multi-level object) are flattern by ‘.’ (dot). Therefore, I can make in html {{user.firstname}} or {{quotation.dateIssued}}.

…but still what I’m searching for is some alternative of for example PHP-latte syntax (Syntax – Latte Templating Engine), because I want to be able to define cycles dynamically (for example I want to have editable html also for table of quotation items, which I have currently fixed in the code).