top of page

Powershell script to automate screen grabs of each page from a book

This night's post shows how you can use a PowerShell script to save each page of an online book as a PDF.


The process involves using an add-in for Chrome called Screenshot Capture.   See: https://chromewebstore.google.com/detail/screenshot-capture/giabbpobpebjfegnpcclkocepcgockkc

 

When this is installed, when you press ALT + S it will automatically save a .png file of the open page to a specified location.  Use these settings, with the option for ‘Capture Viewport’.

  


  


This is the script:

 

Add-Type -AssemblyName System.Windows.Forms

 

$pathToChrome = 'C:\Program Files\Google\Chrome\Application\chrome.exe'

$tempFolder = '--user-data-dir=c:\temp' # pick a temp folder for user data

$startmode = '--start-fullscreen' # '--kiosk' is another option

 

Start-Process -FilePath $pathToChrome -ArgumentList $tempFolder, $startmode, $startPage

 

Start-Sleep -Seconds 5 # Wait for the browser to load

 

for ($i = 0; $i -lt 10; $i++) {

 

    # Press Alt+S

    [System.Windows.Forms.SendKeys]::SendWait('%s')

    Start-Sleep -Milliseconds 5000 # Small delay between key presses

   

    # Press Page Down

    [System.Windows.Forms.SendKeys]::SendWait('{PGDN}')

    Start-Sleep -Milliseconds 5000 # Small delay between key presses

    }

 

 

 Be sure to confirm that the path on the second line is where you have Chrome installed on your PC.


Enter the url of the web page on the line beginning, ‘$startPage’

 

Indicate the number of pages you need to grab here [in this example it’s 10 pages]

 for ($i = 0; $i -lt 10; $i++) {

 

The PowerShell script in effect presses ‘ALT + S’ and then the page down key for you.

 

It’s possible that the script can get thrown off if a page takes a long time to load, so the last part of the script pauses for a set amount of milliseconds.   So 5000 equals 5 seconds.


In Chrome, be sure to enter full screen mode by pressing F11.

 

Just enter the script in the white pane at the top, and then press the play button.



After the script ends and the .png files are generated, you can easily convert them to a single PDF in Adobe Acrobat.


コメント


bottom of page