tcort / wkhtmltox

high performance access to `wkhtmltopdf` and `wkhtmltoimage` from node.js.
61 stars 7 forks source link

Rendered image is much wider than html element #10

Closed NoobDoesMC closed 6 years ago

NoobDoesMC commented 6 years ago

I'm trying to render this page into a png:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <style>
      html, body {
        padding: 0;
        width: 340px;
      }
      html, body, dl {
        margin: 0;
        font-size: 11px;
        font-family: Verdana, Arial;
        background-color: #fff;
      }
      dl {
        padding: 5px;
        width: 330px;
      }
      dt {
        float: left;
        clear: left;
        width: 80px;
        text-align: right;
        color: #1d439b;
      }
      dd {
        width: 240px;
        margin: 0 0 0 90px;
        padding-bottom: 0.5em;
      }
    </style>
  </head>
  <body>
    <dl>
      <dt>eargasm</dt><dd>Audio (music/effects/foley) is outstanding.</dd>
      <dt>中文</dt><dd>My favourite language (mandarin chinese).</dd>
      <dt>eyegasm</dt><dd>Mind-blowingly stunning visuals.</dd>
    </dl>
  </body>
</html>

I am just rendering this like normal:

converter.image(fs.createReadStream('tagslegend.html'), { format: "png" }).pipe(response);

I get an image back like this:

tagslegend-3

It's 1024 pixels wide.

I'm expecting a 340 pixel-wide image.

tcort commented 6 years ago

It defaults to the "screen width" (1024). You can set the width option like this...

converter.image(fs.createReadStream('tagslegend.html'), { format: "png", width: "340" }).pipe(response);

You can run wkhtmltoimage --help to get more options. The command line arguments map to option names, but instead of hyphens, camel case is used... e.g. the command line argument --foo-bar 123 becomes option fooBar: '123'