yumauri / gotenberg-js-client

A simple JS/TS client for interacting with a Gotenberg API
MIT License
111 stars 9 forks source link

How to convert images #16

Closed brucemcpherson closed 4 years ago

brucemcpherson commented 4 years ago

I can't figure out how to convert images to PDF, although other types are working well. All conversions are sent as a stream, and I thought I would be able to use a dataURI method to embed in an html conversion - something like

<html>
<body>
<img src="data:image/jpg;base64,iVBO ... etc" />
</body>
</html>

However, i always end up with an missing image type icon in the final pdf. I also tried writing a temporary file and specifying it as

'img.png': 'file://xyz.png'

and referencing 'img.png' in the html to be converted,but with the same result. Has anybody managed to convert an image stream to pdf ? Would really appreciate an example of how it's done as this is the cleanest pdf convertor I've found for other types of files and I'd love to use it.

yumauri commented 4 years ago

Hello, @brucemcpherson ! Glad you like it :)

According to Gotenberg documentation, you should be able to just add an image file and reference it inside HTML.

So in terms of this client it should be as simple as something like this:

// ---8<---

const toPDF = pipe(
  gotenberg('http://localhost:3000'),
  convert,
  html,
  please
)

const pdf = await toPDF({
  'index.html': `<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My PDF</title>
  </head>
  <body>
    <h1>Hello world!</h1>
    <img src="img.png">
  </body>
</html>`,
  'img.png': fs.createReadStream('img.png')
})

No need to save image to temporary file (if you have it somewhere in the memory), you can just use stream or Buffer directly.

yumauri commented 4 years ago

Setting src as base64 also should work, strange it is not working in your case. I'll try it later today.

brucemcpherson commented 4 years ago

Awesome. It works!

  convertImage({
    inStream,
    outStream,
    mimeType
  }) {
    // make html wrapper
    const toPDF = got.pipe(
      got.gotenberg(this.host),
      got.convert,
      got.html,
      got.please
    );  

      return toPDF({
        'img.png': inStream,
        'index.html': `
        <!doctype html>
        <html lang="en">
          <body>
            <img src="img.png"/>
          </body>
        </html>`
      })
      .then(pdf => pdf.pipe(outStream));
  }

Now I can convert directly to and from google cloud storage.

Thanks so much. I'll try it out with other image types and clean it up.

yumauri commented 4 years ago

@brucemcpherson BTW you can move toPDF const outside of convertImage function ;) So it will not be created every time you convert image.

brucemcpherson commented 4 years ago

Nice - thank you for this brilliant piece of work. It's perfect.

On Wed, 19 Aug 2020 at 09:42, Victor notifications@github.com wrote:

@brucemcpherson https://github.com/brucemcpherson BTW you can move toPDF const outside of convertImage function ;) So it will not be created every time you convert image.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yumauri/gotenberg-js-client/issues/16#issuecomment-675959174, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAOONBDZD7HEKRAMPI36XBDSBOGA7ANCNFSM4QBPZLJQ .