marcbachmann / node-html-pdf

This repo isn't maintained anymore as phantomjs got dreprecated a long time ago. Please migrate to headless chrome/puppeteer.
MIT License
3.56k stars 545 forks source link

incorrect layout display #605

Open MiiZZo opened 3 years ago

MiiZZo commented 3 years ago

The pdf result differs from the original layout, while in some online pdf converters everything works. My nodejs code:

const express = require('express');
const htmlToPdf = require('html-pdf');
const cors = require('cors');

const PORT = process.env.PORT | 3001;

const bootstrap = async () => {
  try {
    const app = express();
    app.use(express.json());
    app.use(cors({
      origin: 'http://localhost:3000'
    }));

    app.post('/', async (req, res) => {
      try {
        const {
          html,
          width,
          height
        } = req.body;

        htmlToPdf.create(html, {
          width: `${width}px`,
          height: `${height}px`,
        }).toStream((err, pdfStream) => {
          if (err) {
            console.log(err)
            return res.sendStatus(500)
          } else {
            res.statusCode = 200

            pdfStream.on('end', () => {
              return res.end()
            });

            pdfStream.pipe(res)
          }
        })
      } catch (e) {
        console.log(e);
      }
    });

    app.listen(PORT, () => {
      console.log('SERVER STARTED');
    });
  } catch (e) {
    console.log('error while server is starting', e);
  }
}

bootstrap();

Initial layout example: https://codepen.io/lucienglue/pen/zYKryzP