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

Background styles not visible on generated PDF #25

Closed
oscarlagatta opened this issue Aug 2, 2017 · 14 comments
Closed

Background styles not visible on generated PDF #25

oscarlagatta opened this issue Aug 2, 2017 · 14 comments

Comments

@oscarlagatta
Copy link

When loading from url the css styles are not taken into consideration

@westy92
Copy link
Owner

westy92 commented Aug 2, 2017

Could you please provide the URL you are using? My guess is that the page doesn't fully render until after the PDF is created. Using a CompletionTrigger should help: https://github.com/westy92/html-pdf-chrome#trigger-render-completion.

@oscarlagatta
Copy link
Author

oscarlagatta commented Aug 3, 2017

This is the code, I added the completionTrigger still not sign of the css

const CDP = require('chrome-remote-interface');
const htmlPdf = require('html-pdf-chrome');

CDP((client) => {
  // Extract used DevTools domains.
  const {Page, Runtime} = client;

  // Enable events on domains we are interested in.
  Promise.all([
    Page.enable()
  ]).then(() => {
      
    return Page.navigate({url: 'http://www.google.com/'});
  });


  // Evaluate outerHTML after page has loaded.
  Page.loadEventFired(() => {
    Runtime.evaluate({expression: 'document.body.outerHTML'}).then((result) => {
      
      const html = result.result.value;
  
      const options = {
               port: 9222, // port Chrome is listening on
               completionTrigger: new htmlPdf.CompletionTrigger.Timer(9000)
        };
      htmlPdf.create(html, options).then((pdf) => pdf.toFile('test2.pdf'));
      //htmlPdf.create(html, options).then((pdf) => pdf.toBase64());
      //htmlPdf.create(html, options).then((pdf) => pdf.toBuffer());
      console.log(html);
      
      client.close();
    });
  });
}).on('error', (err) => {
  console.error('Cannot connect to browser:', err);
});

@tanelih
Copy link

tanelih commented Aug 3, 2017

We had a similar issue where we felt the CSS was not the same as we would see on the page itself. This is actually a "feature" of chrome:

gnab/remark#83

E.g. chrome seems to drop background colors and images when printing.

However if you're not getting any CSS then I don't really know.

@oscarlagatta
Copy link
Author

Just not really understanding if I run the unit tests from within the html-pdf-chrome project and generate the .pdf file still the pdf lacks part of the content from the original web page, eg. www.google.com.

@oscarlagatta
Copy link
Author

I understood the issue, were options parameters missing. How do you bundle the project? After I bundle with gulp-concat the when running shows the following....

SyntaxError: Identifier 'CompletionTrigger' has already been declared
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Function.Module.runMain (module.js:605:10)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3

@GautierT
Copy link

@tanelih : to enable the background pass

await htmlPdf.create(html,  { 
   printOptions: {
      printBackground: true,
  }
})

@tanelih
Copy link

tanelih commented Sep 15, 2017

@GautierT Thanks! We've been using the <body style="-webkit-print-color-adjust:exact;"> trick for now and it seems to work for our needs.

@westy92
Copy link
Owner

westy92 commented Nov 6, 2017

Thoughts on making this the default? @oscarlagatta @tanelih @GautierT

await htmlPdf.create(html,  { 
  printOptions: {
    printBackground: true,
  },
});

@westy92 westy92 changed the title not getting the css styles in the page Background styles not visible on generated PDF Nov 6, 2017
@tanelih
Copy link

tanelih commented Nov 20, 2017

@westy92 The current behavior seems to be pretty much equal to pressing the "print" button in the browser and people might expect this behavior. On the other hand some might find it confusing (including myself at first), that the background image, color or whatever is missing from the output.

If questions or issues arise about this a lot, then I think defaulting to true might be a good idea for the maintainer. 😄

@zimmermanw84
Copy link

zimmermanw84 commented Dec 15, 2017

This CSS hack worked for us. We we're having the same issue around Chrome version 59-60.

* {
	-webkit-print-color-adjust:exact;
}

Edit: Sorry just saw someone else posted a similar solution XD

@JeffHerb
Copy link

The above webkit hack does not seem to work on Linux. Running Chrome 63 on pop os (ubuntu base)

@westy92
Copy link
Owner

westy92 commented Jan 15, 2018

Thank you all for your input. For now, the "official" fix is to specify printBackground: true in your printOptions:

const result = await htmlPdf.create(html,  {
  port: 9222,
  printOptions: {
    printBackground: true,
  },
});

If an overwhelming number of users end up specifying this, I may make it the default in a future release.

Other possible fixes:
CSS:

* {
  -webkit-print-color-adjust: exact;
}

Inline CSS:

<body style="-webkit-print-color-adjust: exact;">

@patrickehansen
Copy link

This issue is still alive.
The CSS fix doesn't work for me, and the printOptions.printBackground doesn't work either.

@1kastner
Copy link

I currently try to do the same thing from the CLI. Does anybody know how to pass the options there?

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

8 participants