Print web page to PDF

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
gregpet
Newbie
Posts: 13
Joined: Wed May 22, 2013 8:11 pm

Print web page to PDF

Post by gregpet » Wed Jun 05, 2013 2:18 pm

I am looking to print a web page to a pdf document. Everything works until I get to putting the file name in the dialog box. Tried several methods. This is the latest.

//print screen
Day>dd
Month>mm
Year>yyyy
lettrim(%dd%),dd
VBEval>trim(%mm%),mm

Let>today=%mm%-%dd%-%yyyy%
let>FN=C:\Users\GPetersen\Documents\Banking\BoW Accoount Summary ,%today%

SetFocus>My Accounts - Windows Internet Explorer
Press CTRL
Let>SK_DELAY=10
Let>SK_IGNORECAPS=0
SendText>p
//Find
FindImagePos>%BMP_DIR%\image_2.bmp,WINDOW:Print,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
MouseMove>XArr_0,YArr_0
Endif

//Find and Left Click Center of
FindImagePos>%BMP_DIR%\image_1.bmp,WINDOW:Print,0.7,1,XArr,YArr,NumFound,CCOEFF
If>NumFound>0
MouseMove>XArr_0,YArr_0
LClick
Endif

This is where it hangs at SK_DELAY when I step thru it.
Press Enter
Let>SK_DELAY=5
Let>SK_IGNORECAPS=1
SendText>C:\Users\GPetersen\Documents\Banking\BoW Accoount Summary ,%today%

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Post by hagchr » Wed Jun 05, 2013 9:39 pm

Hi, I also print to pdf regularly and then to simplify and make my macros more robust I usually set "Adobe PDF" as the default printer. Then it is automatically pre-selected and there is no need for the image recognition. Then for your case it would just be a simple macro like below. (alternatively if you want to select the printer each time you can use the image recognition as you do but need to make sure the focus is right by using eg WaitWindowOpen and SetFocus. I am not sure how the screen looks in your case).

(Update - I just realized that once the select printer window is open, if I send the text "Ado", ie the first characters of the printer name (Adobe PDF) it will select the right one. This could also be a way to select the proper printer without need for using the image recognition if you want to select the printer each time).

Day>dd
Month>mm
Year>yyyy
Let>today=%mm%-%dd%-%yyyy%

//Web page in question
SetFocus>My Accounts - Windows Internet Explorer
Wait>0.1

Press>CTRL
SendText>p
Release>CTRL

WaitWindowOpen>Print
Wait>0.1

SetFocus>Print
Wait>0.1

Press>ENTER
Wait>0.1

WaitWindowOpen>Save PDF File As
Wait>0.1

Let>FN=C:\Users\GPetersen\Documents\Banking\BoW Account Summary,%today%.pdf
SendText>FN
Wait>0.1

Press>ENTER

gregpet
Newbie
Posts: 13
Joined: Wed May 22, 2013 8:11 pm

Post by gregpet » Thu Jun 06, 2013 4:16 pm

This is where it is getting stuck.

WaitWindowOpen>Save PDF File As

SendText>FN
wait>0.1
Press Enter

the text always ends up in the search box at the top of the window even if I try using image recognition and mouse commands to highlight the file name box. It almost acts like the focus is being changed on each code line then when the window is activated causing the search box to be the first one active at each line.

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Fri Jun 07, 2013 12:28 am

Can you throw in a tab or two to change from the search box to the file name field.
FIREFIGHTER

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Fri Jun 07, 2013 1:59 pm

As far as I know WaitWindowOpen> just controls program execution, it doesn't automatically focus on the window so I believe you would need a

SetFocus>

on the window before sending it text.

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Post by Grovkillen » Sun Jun 09, 2013 8:57 am

Have you tried the wkhtmltopdf I posted some time ago?

It'll let you throw the URL into the app and give you a rendered PDF file back...
Let>ME=%Script%

Running: 15.0.27
version history

gregpet
Newbie
Posts: 13
Joined: Wed May 22, 2013 8:11 pm

Post by gregpet » Thu Jun 13, 2013 2:16 pm

I think using that is above my pay grade (knowledge). I looked at it and I am not sure how to use it.

If anyone has got some example code to print to PDF I would appreciate looking at it.

thanks everyone for looking at this

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 13, 2013 2:48 pm

This works for me. Sends the current FireFox screen (using the Adobe PDF print driver) to a pdf file in the temp folder.

Code: Select all

SetFocus>Mozilla Firefox*
Wait>1

Press ctrl
Send>p
Release ctrl

WaitWindowOpen>Print*
Wait>1
Send>apo
Wait>1

SetFocus>Print*
Wait>1

Press Enter

WaitWindowOpen>Save PDF File As*
Wait>1

Send>%temp_dir%FireFoxScreenPrint.pdf
Wait>1

Press Enter
Last edited by JRL on Thu Jun 13, 2013 3:38 pm, edited 1 time in total.

hagchr
Automation Wizard
Posts: 331
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Post by hagchr » Thu Jun 13, 2013 3:04 pm

Sometimes I have had problem with hidden windows that could confuse what is in focus. Both SetFocus and WaitWindowOpen have the choice to look for visible windows. Not sure if that is an issue here. I have updated my previous code (below) and it works on my machine without any problem.



Day>dd
Month>mm
Year>yyyy
Let>today=%mm%-%dd%-%yyyy%

//The open web page to be printed
SetFocus>Print web page to PDF*

Wait>0.1
Press>CTRL
SendText>p
Release>CTRL

//Adjust to just look for visible windows
Let>WF_TYPE=2
WaitWindowOpen>Print
Wait>0.1

SetFocus>Print
Wait>0.1

//My printer is called "Adobe PDF" so I send the first letters to select the printer
SendText>Ado
Wait>0.1

Press>ENTER
Wait>0.1

//Adjust to just look for visible windows
Let>WF_TYPE=2
WaitWindowOpen>Save PDF File As
Wait>0.1

SetFocus>Save PDF File As
Wait>0.1

//Path in FN to be replaced with your data
Let>FN=C:\Users\Christer\Desktop\BoW Account Summary, %today%.pdf

SendText>FN
Wait>0.1
Press>ENTER

User avatar
Grovkillen
Automation Wizard
Posts: 1131
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Post by Grovkillen » Fri Jun 14, 2013 6:49 am

gregpet wrote:I think using that is above my pay grade (knowledge). I looked at it and I am not sure how to use it.

If anyone has got some example code to print to PDF I would appreciate looking at it.

thanks everyone for looking at this
It's not that complicated... just ask me. Download the app and place it in your script directory. Run this code:

Code: Select all

Let>OUTPUT_FILE=OUTPUT_HTML.pdf
Let>URL=http://www.vecka.nu
Let>RP_WAIT=1
//set RP_WINDOWMODE=0 if you don't want the command window visible
Let>RP_WINDOWMODE=1
IfFileExists>%SCRIPT_DIR%\%OUTPUT_FILE%
DeleteFile>%SCRIPT_DIR%\%OUTPUT_FILE%
Endif>
RunProgram>%SCRIPT_DIR%\wkhtmltopdf.exe %URL% %OUTPUT_FILE%
Label>START
IfFileExists>%SCRIPT_DIR%\%OUTPUT_FILE%
ExecuteFile>%SCRIPT_DIR%\%OUTPUT_FILE%
Else>
Goto>START
Endif>
End>0
EDIT: just my personal opinion but the "Wait>" command is to me a small failure becasue it indicates that the script does not know what the computer is doing during that time interval. I try to only use "Wait>" when waiting for user interaction, i.e. press a keyboard combination that trigger a script... Or if it's a none human interaction script that need to be run in intervals...
Let>ME=%Script%

Running: 15.0.27
version history

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts