Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a polka/express implementation #6

Open
soullivaneuh opened this issue Feb 1, 2020 · 9 comments
Open

Add a polka/express implementation #6

soullivaneuh opened this issue Feb 1, 2020 · 9 comments

Comments

@soullivaneuh
Copy link

Subject

It would be useful to have one example of how to bind this client to a polka and/or express server.

A complete example with a url extension matching .pdf would be enough for a good demo.

Concrete case

I use the Sapper framework which has ssr rendering.

I can create a .pdf.svelte route with html rendering.

I would like to match this kind of route to wrap the renderd html on a pdf file.

@yumauri
Copy link
Owner

yumauri commented Feb 1, 2020

Hello!
Never heard about polka (this one, right?), I'll check it out, thank you :)

Can you explain what do you mean by "bind this client to a express server"?

There is example in README, how you can send PDF file to client from Express server:

app.get('/pdf', function(req, res) {
  //...
  pdf.pipe(res)
})

And I don't quite understand, what is "wrap the renderd html on a pdf file"?

Can you explain step-by-step, how do you see it, please?

@soullivaneuh
Copy link
Author

Thanks for the quick reply!

Oh right, the express example should match as polka is based on express. 👍

And I don't quite understand, what is "wrap the renderd html on a pdf file"?

I have this link: /posts/1 that renders the html page.

I would like /posts/1.pdf to render the pdf version of the page.

I also use Sapper middleware and I'm quite confuse about how I should make this working. 🤔

@yumauri
Copy link
Owner

yumauri commented Feb 3, 2020

I've never used Sapper, so I made a quick investigation.

I guess you can add your own middleware, right before sapper.middleware(), and intercept all requests to *.pdf.

But I cannot tell, how to return PDF from Sapper middleware... As far as I can tell from its source code — it never calls next() in case of matching page, so any middleware after sapper.middleware() will not be executed. I tried to replace res.end, but without success. It is possible to get page HTML content, but I don't know, how to replace response with PDF :(

Here is what I tried, you can dig in that direction, maybe you will succeed:
https://pastebin.com/NB7HdfHQ (this is file src/server.js)

@yumauri
Copy link
Owner

yumauri commented Feb 3, 2020

Hah! I made it with a little kinda hack — immediately replace res.end function back, when it is called, so it will not go recursive: https://pastebin.com/ETxkTG4n

Screenshot 2020-02-03 at 20 43 50

@yumauri
Copy link
Owner

yumauri commented Feb 3, 2020

To have styles, you should also add them to gotenberg, as sources. Looks like sapper generates them dynamically, so, this is whole another task to get links to styles from HTML content, and add those styles to gotenberg.

And I doubt it should be documented, this is quite specific use case, in my opinion...

@soullivaneuh
Copy link
Author

Wow, I'm impress by your POC! I'll look at that and try my integration ASAP! Thanks a lot! 👍

And I doubt it should be documented, this is quite specific use case, in my opinion...

Maybe not directly on the README, but maybe a cookbook section? I would enjoy to add my contribution if I manage to make something right with this method. 😉

@yumauri
Copy link
Owner

yumauri commented Feb 6, 2020

Is your project open source? I think I can add links to projects, which are using this library, somewhere...

@soullivaneuh
Copy link
Author

soullivaneuh commented Feb 22, 2020

Unfortunately, not this one.

Your PasteBin works great, but it's a lot of setup just to convert a page to a4 pdf format.

Don't you think some parts of your script may be provided by your library? 🤔

The goal would be to simplify the integration, and make the users happier. 👍

@yumauri
Copy link
Owner

yumauri commented Feb 24, 2020

Hm.

There are only 13 rows, related to PDF converting (including import) in the whole pastebin example.

I can think of something like Gotenberg Express middleware, which will convert responses to PDF on the fly, but this definitely should be external package. Which could use my library :)

I don't think this should be in this client, I like Unix philosophy, when one tool do one thing and do it well :)

But I'll mark this idea with middleware in my list :)
And you are welcome to implement this package ahead of me ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants