How to PDF (or print) a WebBrowser Object

It’s been necessary for me to generate PDF reports of a large number of tables with charts.

Charts are created in Panorama by using Plotly within a WebBrowser object. Printing or saving a PDF containing a WebBrowser object generates blanks in place of the WebBrowser.

Screenshots became the apparent solution, but what a tedious job if you have multiple pages that subsequently require assembly in some other application.

The solution: automate screenshots in Panorama, save them to a targeted destination as PDFs with defined dimensions, then assemble them into a single PDF document.

The process begins by making sure the form you’re working from is properly sized. Depending on the settings of the Toolbar, the top of the window’s interior can vary so I start by making sure I know the size of the Toolbar and then size the whole window to my exact needs.

Local a,b,c,d
GetWindow a,b,c,d
ZoomWindow a,b,525,785,“notoolbar noscroll”

These dimensions will cause the screen capture to include the entire window, Toolbar and all, but since I now know the size of the Toolbar, I crop it out of the screenshot and crop off the rounded corners at the bottom of the window.

Then I assemble a Shellscript where the a,b,c,d values are required in a different order. In this example, I append a number to the end in order to be able to create a few without any overwrites. In this case it’s being saved to the Desktop.

Local a,b,c,d
GetWindow a,b,c,d
Zoomwindow a,b,524,784,"notoolbar noscroll"
;crop out the drag bar and the rounded corners of the window
a = a+30
c = c-30
b = b+7
d = d-14

Let lvDimensions = b+{,}+a+{,}+d+{,}+c
Let lvSuffix = str(Now())[-2,-1]

shellscript |||screencapture -t pdf -T 1 -R|||+lvDimensions+||| ~/Desktop/ScreenCapture|||+lvSuffix+|||.pdf|||

Obviously there are a number of changes in order to make it yours, but from here it’s easily adaptable.

Mine cycles through a number of forms, saving each with a sequential number as the suffix. Once that’s done I have a Python script that joins them all into one document. In some cases the procedure zips it with a password then emails it.

There’s much more available in the syntax for screencapture. You can find An A-Z Index of the Apple macOS command line

1 Like