Print PDF with headless chrome in ubuntu - google-chrome

I'm trying to print a pdf in ubuntu using chrome headless with this command line :
google-chrome --headless --disable-gpu --print-to-pdf=gen2.pdf file:///home/user/Desktop/invoiceTemplate2.html
The file is generated, but without any text and all other styles are applied.
And when I print the same page HTML with chrome directly, it prints ok.
And I've tried to print the same file HTML on windows with headless chrome and it did print ok with the letters and all.
And when I try to print a HTML file but from the internet like :
instead of : file:///home/user/Desktop/invoiceTemplate2.html
I put : http://www.google.com
it prints OK with headless.
Any help will be apreciated, thx.

Related

How to use brave to automate printing html to pdf?

I currently manually open HTML files using Brave Browser and print the files to pdf files. I want to automate this process in the command line. Is there a way to do it?
Since Brave is based on chromium, solutions based on chromium and google-chrome are also welcome.

How to set Headless Chrome wait until the page is fully loaded before printing?

The command to use headless Chrome to print webpage as PDF is chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/. The wkhtmltopdf has a feature --window-status ready_to_print to do so.
I ask this question because when I print this webpage contains MathJax.js, it has an error as follows:
The page source of that webpage has the following lines related to mathjax
<script src="../scripts.js"></script>
<script src="../../MathJax/MathJax.js?config=TeX-AMS_SVG"></script>
I would like to ask if there is an option so that Headless Chrome will wait until the page is fully loaded before printing?
We can use two options to achieve this goal: --run-all-compositor-stages-before-draw and --virtual-time-budget=10000.

chrome disable security messed chrome functioning

google image
Hi, stackoverflow, i am trying to debug a react native app on a Mac and when i open chrome in disabled security mode, the data rendered by google chrome is not normal.
google resultas are not shown, and the design of chrome get messed (the image above)
this is the command : open /Applications/Google\ Chrome.app --args --disable-web-security --user-data-dir
You are probably using Chrome 67.
This problem has been fixed in the last Canary, so you can:
wait for the next Chrome
use an old version (e.g. install Chromium 66)
or you can:
Add the --disable-site-isolation-trials argument to chrome via https://docs.cypress.io/api/plugins/browser-launch-api.html#Usage
Reference: https://github.com/cypress-io/cypress/issues/1951#issuecomment-401579981

additional options in Chrome headless print-to-pdf

I need help one more time. I am trying to print a page to pdf using headless feature of the chrome. However, header and footer is present in the pdf. I found that this option as been implemented in Devtools.
https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
However, i can't find how can i use these options in CLI. Also is it possible to invoke the Devtools from selenium?
Additionally how can i invoke Page.PrintToPDF in Dev tools. I tried to run the command in Console. It is showing Page is undefined.
Add this CSS to the page your creating into a PDF to remove Chrome Headless's implemented Header and Footer.
CSS:
#media print {
#page { margin: 0; }
body { margin: 1.6cm; }
}
You should format your command like below to create the PDF:
"C:\PATH\TO\CHROME\EXECUTABLE\FILE", "--headless","--disable-gpu","--print-to-pdf=" + directory path to where you want the file to go followed by the desired file name/fileName.pdf,"--no-margins", "the path to the file you want turned into a pdf"
Example 1:
C:\chrome-win/chrome --headless --disable-gpu --print-to-pdf=C:\user\fileName.pdf --no-margins C:\Projects\index.html
Example 2:
You can also test this functionality by navigating in your command line to the folder containing Chrome executable file, and running this command:
chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/
"/path/to/google-chrome" : This is the path of Google Chrome.
'--headless' : Chrome browser in a headless environment without the full browser UI
'--run-all-compositor-stages-before-draw' : It Prevents the Pdf Creation Before all the data is rendered(After all data is rendered the pdf will be created).
'--virtual-time-budget = x: It Delays the Process of creation of Pdf, here x will be the miliseconds.
'--print-to-pdf' : This Flag creates the pdf of the given Url.
URL : The url of webpage.
PDF Page Formatting (Using CSS)
Adding this(to css files):
#media print {
   #page { margin: 0mm 0mm 0mm 0mm;
size:8in 9.5in;
}
}
The Above CSS code has no effect on the Webpage Rendering,But effect on the formatting of page in PDF only.
If you need to print a page to PDF using Chrome (or Edge), without header and footer, there is an additional command line option: --print-to-pdf-no-header.
For a comprehensive list of all command line options, there is:
https://peter.sh/experiments/chromium-command-line-switches/
Yes this media query needs to be added if you want to download the webpage as pdf.
Reference
Media Print CSS
https://developer.mozilla.org/en-US/docs/Web/CSS/#media
Page CSS
https://developer.mozilla.org/en-US/docs/Web/CSS/#page

Registering chromium-browser for MarkDown preview plug-in in Sublime Text 2 fails

Using Linux, Sublime 2, open markdown inside chromium instead firefox (or other).
There are suggestions to set
webbroser.register('chromium-browser', ??)
and then use
webbrowser.get('chromium-browser').open("file://" + output.name)
instead of
webbrowser.open("file://" + output.name)
in MarkdownBuild.py to build html from markdown into chromium-browser instead default (firefox). But I can't get it working...
Update
cat ~/.zshrc : export BROWSER=chromium-browser > $BROWSER shows up a new window of chromium. All links from skype or any chat are retrieved by chromium. But still sublime Ctrl+B opens Firefox.