Print web page to PDF
Moderators: Dorian (MJT support), JRL
Print web page to PDF
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%
//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%
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
(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
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.
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.
- CyberCitizen
- Automation Wizard
- Posts: 721
- Joined: Sun Jun 20, 2004 7:06 am
- Location: Adelaide, South Australia
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
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...
It'll let you throw the URL into the app and give you a rendered PDF file back...
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.
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
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
- Grovkillen
- Automation Wizard
- Posts: 1131
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
It's not that complicated... just ask me. Download the app and place it in your script directory. Run this code: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
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