How Do I Get Route HTML Response String for PDF Conversion?

Hi all!

I’ve been working on this issue for the last couple of days. I’m creating a custom module to convert HTML to PDF.

I have a route, with parameters, to a template file (receipt) that I would like loaded and rendered in the background with the resulting html response as a string that I could then pass into a HTML to PDF converter.

The problem I’m facing is 3 faceted.

  1. I cannot figure out how to route to the template without the browser loading the response (or even route at all from the custom module to be honest).
  2. When using node-fetch, I get stuck at my login page (potentially session issues??)
  3. Not understanding how serverconnect is running things isn’t helping either.

I was using puppeteer, but I find it very finicky and having to save login details somewhere, then fill out the login forms, not the most secure way to do things.

I’ve tried routes app.get (to no avail), I’ve tried node-fetch, I’ve tried puppeteer and keep ending up at the login page.

Any guidance is greatly appreciated.

Hi. It seems what you are trying to do is a bit complex.
We created a simple & free custom module for HTML to PDF using Pupperteer for NodeJS.
Maybe this will give you some ideas, in case you haven’t seen this before: HTML to PDF Action - NodeJS & PHP [Open Source]

Hey Sid,

Yep, complexity is becoming my issue.

I started to use HTML to PDF and was able to get it to work (started my own module based off yours for puppeteer), but I’d like a little more flexibility around the templating for the reports. Not only am I going to need to generate invoices in the future, I’ll also need to template accounting reports, class reports, deck supervisor class reports, class instructor reports etc… with many display, date and time options too. The program will eventually be used for a Swim School.

The custom module is like any other step which needs inputs & generates a PDF.
For all types of reports you wish to generate, you just need to create the HTML, and then pass that to this step.
All custom options & designs is part of the HTML buidling, and not the custom module.

Oh I understand that. The HTML building is where I want to just get the HTML output of a route rather than having to do the steps I have in the screenshot below. Then I can just have a bank of report templates with all the serverconnects, repeats, CSS and what not that I need included in the EJS file, then pump the resulting HTML string into the module.

I follow.
This is something we played around with as well, but could not come up with a solution at that time due to security concerns.
If you are going to create a route for source of your report, you should rather think of it as a regular page. Then, modify the custom module to just load that page instead of passing HTML to it.
Only thing you will have to figure out is how you secure that page so that puppeteer can run it, but no one else.

Yeah, that’s the issue. Puppeteer being a new instance of a browser means logging in and for the life of me I can’t work out how to pass the session on the client side to puppeteer. That would make life a lot easier. Just pass the url with Params filled in and serverconnect will load the data.

I’ve thought up a few options I’m hoping to test out. First one is to create an action in the library, include it in the API action, parse the resulting JSON and use EJS to render a template from file with the JSON data and pass this through, or setup a custom route (gotta figure that one out though…) and pass the res.send(html) through to puppeteer.

I’ll admit, the custom route path scares me :joy:

You can do that by extending Express: PREVIEW: Extending express (NodeJS)

Not sure how you will pass the HTML though.

Yep… Doing my head in!

At least I know it hasn’t just been me and my very intermediate node/express skills trying to find a solution to passing a template through to Puppeteer. If we could pass a session, that bypasses the login form securely, that would be a much better option.

Thanks for your insights. I’m off to code some EJS templates to pass JSON to.

Well after doing a crash course in EJS, which is quite easy, I’ve been able to come up with a solution with Puppeteer.

Passing data from a library action into my api action, then inputting this JSON string into an EJS renderFile function that passed into Puppeteer as a HTML string.

I’m happy with this. It’s all server side and no need to pass credentials around OR worry about routes and express. Just server connect and template building once I finish off the module.

Don’t mind the layout or information. This was a quick test. I also haven’t used any options like headers or margins or paper size as of yet.

1 Like

Could share some info on this? Could be really useful.

Absolutely. I’ve literally just finished writing the final of the module. So give me a bit and I might put it into a how to. :slight_smile:

2 Likes

have you had any time for the how to? I would like to pass some data from the API action into html to render as pdf. Just got the pdf create to work in docker with puppeteer.

This is the module shared by @mikkime23: MODULE: Generate PDF from EJS Template with Puppeteer

1 Like