About to lose my mind trying to get Page.printToPDF to work form an extension. What am I doing wrong?

2,747 views
Skip to first unread message

Sam Putnam

unread,
May 30, 2019, 8:55:33 PM5/30/19
to Chromium Extensions
Somebody please help!!!! =.... (

https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF


chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {

 
    if (changeInfo.status == 'complete' && tab.status == 'complete' && tab.url != undefined) 


chrome.debugger.attach({tabId:tabId}, "1.2", function(){
chrome.debugger.sendCommand({tabId:tabId}, "Page.printToPDF", {}, function(blahs){
console.log(blahs);
});

});

}

});

I've tried everything I can think of to get this to work. I even launched 10 different versions of chrome ranging from version 52 to 68, restricting auto update using group policies. 

If chrome accepts the command it returns the following: Unchecked runtime.lastError: {"code":-32000,"message":"PrintToPDF is not implemented"}

headless wont accept the paramters I want to use which are the following:
 

landscape
booleanPaper orientation. Defaults to false.
displayHeaderFooter
booleanDisplay header and footer. Defaults to false.
printBackground
booleanPrint background graphics. Defaults to false.
scale
numberScale of the webpage rendering. Defaults to 1.
paperWidth
numberPaper width in inches. Defaults to 8.5 inches.
paperHeight
numberPaper height in inches. Defaults to 11 inches.
marginTop
numberTop margin in inches. Defaults to 1cm (~0.4 inches).
marginBottom
numberBottom margin in inches. Defaults to 1cm (~0.4 inches).
marginLeft
numberLeft margin in inches. Defaults to 1cm (~0.4 inches).
marginRight
numberRight margin in inches. Defaults to 1cm (~0.4 inches).
pageRanges
stringPaper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
ignoreInvalidPageRanges
booleanWhether to silently ignore invalid but successfully parsed page ranges, such as '3-2'. Defaults to false.
headerTemplate
stringHTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:
  • date: formatted print date
  • title: document title
  • url: document location
  • pageNumber: current page number
  • totalPages: total pages in the document
For example, <span class=title></span> would generate span containing the title.
footerTemplate
stringHTML template for the print footer. Should use the same format as the headerTemplate.
preferCSSPageSize
booleanWhether or not to prefer page size as defined by css. Defaults to false, in which case the content will be scaled to fit the paper size.


Abro

unread,
May 31, 2019, 2:11:50 AM5/31/19
to Chromium Extensions
As it says printtopdf does not exist.

Sam Putnam

unread,
May 31, 2019, 3:02:14 AM5/31/19
to Chromium Extensions
So you're saying that chrome doesn't support google's own protocol? Why are they documenting a feature if it doesn't exist. I've seen post of people referencing having used it in previous instance of chrome. I just haven't been able to replicate it by downgrading the browser. It has to exist somewhere doesn't it?

馮韋元Francois Devatine

unread,
May 31, 2019, 3:54:29 AM5/31/19
to Sam Putnam, Chromium Extensions
Can you isolate the problem a little?
The API you are using is chrome.debugger and using sendCommand function.
This will send a command to a tab. That part works, it does send the command to the tab.

From that point on, it's not about Chrome extension, it's simply wether the tab knows the command you are trying to execute or not.
If you just go to the Console and type the command, does it work?

I feel the problem is not the chrome extension.
It's the fact that chrome.debugger is only made to support Debugger methods (https://chromedevtools.github.io/devtools-protocol/tot/Debugger) and you are trying to use the Page methods (https://chromedevtools.github.io/devtools-protocol/tot/Page).

Otherwise, sending window.print() would open the print window, and from there you could choose to save as PDF.

Cheers,
Francois

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/6765e3c5-642a-4321-a2fe-a12dd9b9f447%40chromium.org.

Sam Putnam

unread,
May 31, 2019, 4:38:36 AM5/31/19
to Chromium Extensions, putna...@gmail.com
I've tried using chrome.debugger.sendCommand with other page methods and they have all worked just fine...

"It's the fact that chrome.debugger is only made to support Debugger methods"

sendCommand allows you to interact with the full chrome dev tool protocol, the sole exception seems to be PrintToPDF.

 



On Friday, May 31, 2019 at 2:54:29 AM UTC-5, 馮韋元Francois Devatine wrote:
Can you isolate the problem a little?
The API you are using is chrome.debugger and using sendCommand function.
This will send a command to a tab. That part works, it does send the command to the tab.

From that point on, it's not about Chrome extension, it's simply wether the tab knows the command you are trying to execute or not.
If you just go to the Console and type the command, does it work?

I feel the problem is not the chrome extension.
It's the fact that chrome.debugger is only made to support Debugger methods (https://chromedevtools.github.io/devtools-protocol/tot/Debugger) and you are trying to use the Page methods (https://chromedevtools.github.io/devtools-protocol/tot/Page).

Otherwise, sending window.print() would open the print window, and from there you could choose to save as PDF.

Cheers,
Francois

On Fri, May 31, 2019 at 3:02 PM Sam Putnam <putn...@gmail.com> wrote:
So you're saying that chrome doesn't support google's own protocol? Why are they documenting a feature if it doesn't exist. I've seen post of people referencing having used it in previous instance of chrome. I just haven't been able to replicate it by downgrading the browser. It has to exist somewhere doesn't it?

On Friday, May 31, 2019 at 1:11:50 AM UTC-5, Abro wrote:
As it says printtopdf does not exist.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.

Apy.fyi Admin

unread,
Dec 22, 2023, 4:23:48 PM12/22/23
to Chromium Extensions, Sam Putnam
Hey Sam were you able to figure this out? 
Reply all
Reply to author
Forward
0 new messages