chrome headless print-to-pdf add param for the index file

92 views
Skip to first unread message

Karim Double A

unread,
Apr 2, 2023, 8:13:30 AM4/2/23
to Chromium-discuss
I'm trying to get a chrome headless script working where I convert a local html file to a pdf file. But within the HTML file I would like to pass some parameters to use in my html file. Unfortunately this seems easier said than done as I can't find much information on this.

This command line works correctly

    google-chrome --headless=new --no-sandbox --disable-gpu --print-to-pdf=file2.pdf file2.html

And this is my file2.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    </head>
   
    <body >
   
    <div class="page" id="page-0">
    <div id="content-0">
    <div id="prm"></div>
    </div>
    </div>
   
    </body>
   
    <script>
    const queryString = window.location.search;
   
    const urlParams = new URLSearchParams(queryString);
    const product = urlParams.get('product')
   
    document.getElementById("prm").innerHTML = product
    </script>
   
    </html>

The script within file2.html works properly, I tested this myself locally. The problem I run into is that chrome headless changes the url every time.

For Example:

    google-chrome --headless=new --no-sandbox --disable-gpu --print-to-pdf=file2.pdf file2.html\?product\=test
**Becomes:** file2.html/?product=test  (So not found because of "/" after .html)
 
    google-chrome --headless=new --no-sandbox --disable-gpu --print-to-pdf=file2.pdf file2.html\#product\=test
**Becomes:** file2.html/#product=test  (So not found because of "/" after .html)


In addition, I also found an extra parameter argument that I could use according to the [documentation][1] and that was "extra-search-query-params".

So I tried the following command:

    google-chrome --headless=new --no-sandbox --disable-gpu --print-to-pdf=file2.pdf file2.html --extra-search-query-params="product=test"

But no luck either.

I can use the complete url with domain name and then it does work. Only the html page will eventually be used from the backend and not from a public url address.

Does anyone know how I can pass a parameter to my local index html page from the command line?

Thank you in advance.

  [1]: https://peter.sh/experiments/chromium-command-line-switches/#show-autofill-signatures
Reply all
Reply to author
Forward
0 new messages