yumauri / gotenberg-js-client

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

Enabling `printBackground` for html to PDF conversion? #33

Closed ghost closed 3 years ago

ghost commented 3 years ago

I would like to enable the printBackground option when creating a PDF with HTML. In Gotenberg's documentation, we can pass a param in the form data to enable this: https://gotenberg.dev/docs/modules/chromium How can i do the same with gotenberg-js-client?

ghost commented 3 years ago

This is how to do it:

  const toPDF = pipe(
    gotenberg(url),
    convert,
    html,
    adjust({
      fields: {
        printBackground: true,
      },
    }),
    please
  );

however, the typings are not updated for this yet.

Type '{ printBackground: true; }' is not assignable to type 'RequestFields'.
  Object literal may only specify known properties, and 'printBackground' does not exist in type 'RequestFields'.
yumauri commented 3 years ago

Hello!

Gotenberg:7 was released 5 days ago, and this library is not yet compatible with it. There is an issue for that.

It is possible to manually adjust request, as you already figured that out :) This doesn't looks good, though, I know...

const toPDF = pipe(
  gotenberg(''),
  convert, // it is save to remove this line, if you want
  html,
  adjust({
    // manually adjust endpoint, because
    // gotenberg:7 has different conversion endpoints
    url: 'http://localhost:3500/forms/chromium/convert/html',

    // manually adjust for fields
    fields: {
      printBackground: true,
      // `printBackground` is not valid field for gotenberg:6
      // so we have to cast to any, otherwise typescript will complain
    } as any,
  }),
  please
)

I've tested, and this works fine.

yumauri commented 3 years ago

Oh, you don't have to adjust URL if you add /forms/chromium to gotenberg function argument:

pipe(
  //         / gotenberg url:port \
  gotenberg('http://localhost:3500/forms/chromium'),
  convert,
  html,
  adjust({
    fields: {
      printBackground: true,
    } as any,
  }),
  please
)
ghost commented 3 years ago

Thanks @yumauri! This library is really good - thank you.