Google Answers Logo
View Question
 
Q: Need to find a program that can print a web page from a command-line ( Answered 4 out of 5 stars,   2 Comments )
Question  
Subject: Need to find a program that can print a web page from a command-line
Category: Computers > Software
Asked by: timtitus-ga
List Price: $25.00
Posted: 30 Aug 2005 10:59 PDT
Expires: 29 Sep 2005 10:59 PDT
Question ID: 562270
I want to print a web page on my printer every week, so I would like
to put this in my Windows scheduler to run automatically.  For
example:

printweb http://news.google.com

This would print out Google's news web page on my local printer.  It
should run from a command-line without any GUI so it will run in the
background.

Please help me locate a program that will do this.
Answer  
Subject: Re: Need to find a program that can print a web page from a command-line
Answered By: sublime1-ga on 31 Aug 2005 19:29 PDT
Rated:4 out of 5 stars
 
tim...

This script I found on Eric Phelps website does the trick for me.
I'll reproduce it here, slightly modified to fit the format of GA:

CODE

Dim wrd 'As Word.Application
Dim doc 'As Word.Document
Dim EndWait 'As Double

If MsgBox ("This program illustrates how to automatically
print a web page by automating Microsoft Word. BUT -- it
will only work if you installed the HTML filters for Word
and you have an internet connection! OK to continue?",
vbOkCancel) = vbCancel Then Wscript.Quit

Set wrd = CreateObject("Word.Application")

'''''''''' Remove the below line if you don't want to watch Word 
wrd.Visible = True

'''''''''' Replace the web page with the one you want printed.
Set doc = wrd.Documents.Open("http://www.ericphelps.com/")

'''''''''' This is a messy kluge to give the page time to load
'''''''''' If you try to print before the page is loaded, Word crashes!
EndWait = Now + (30 / 86400) ' 30 second delay to let page load
Do Until Now > EndWait
Loop

'''''''''' Un-comment the below line if you want to use a printer
'''''''''' other than your default. If you specify a printer,
'''''''''' it must be one that is already installed on your system.
'wrd.ActivePrinter = "HP LaserJet III"

doc.PrintOut
doc.Close
wrd.Quit
Set wrd = Nothing

/CODE


The MsgBox entry will require a response, so if you know you 
have the HTML filters for Word installed, you can delete it,
or comment it out, as follows:

'If MsgBox ("This program illustrates how to automatically
'print a web page by automating Microsoft Word. BUT -- it
'will only work if you installed the HTML filters for Word
'and you have an internet connection! OK to continue?",
'vbOkCancel) = vbCancel Then Wscript.Quit

As it is, the script is set to show Word's activities, but as
is noted in the script, you can remove the line which does that:
wrd.Visible = True

...or just comment it out:
'wrd.Visible = True

You might want to run through a supervised version first, with
Word visible.

If the filters are not installed, Word may ask you if you want
to install them from the internet on the spot.

And, of course, make sure you schedule it to run when you're
connected to the internet, and that your printer is turned on.

The script can be downloaded in text, vbs, or zip format, with
a readme.txt file from this page on Eric's site:
http://www.ericphelps.com/scripting/samples/PrintWebPageWithWord/index.html

Eric's other scripts for Windows can be found here:
http://www.ericphelps.com/scripting/samples/


Please do not rate this answer until you are satisfied that  
the answer cannot be improved upon by way of a dialog  
established through the "Request for Clarification" process. 

sublime1-ga


Additional information may be found from an exploration of
the links resulting from the Google search outlined below.

Searches done, via Google:

scheduler script "print a web page"
://www.google.com/search?q=scheduler+script+%22print+a+web+page%22

Request for Answer Clarification by timtitus-ga on 01 Sep 2005 07:35 PDT
It looks like you're 90% of the way there.  What do I save this
program as?  I presume it should be saved as "printdoc.vbs"?  How do I
call the program and enter the command line?

printdoc.vbs http://news.google.com

Will this print the web page from the command line?

Clarification of Answer by sublime1-ga on 01 Sep 2005 12:48 PDT
tim...

You asked:

What do I save this program as?  I presume it should be saved as
"printdoc.vbs"?

You can name it anything you like, with a .vbs extension.


How do I call the program and enter the command line?

You mentioned wanting to use Windows Scheduler.
If you open the Windows Scheduler from the Control Panel,
one of the pages of the Wizard dialog will ask you to 
navigate an Explorer-type interface and locate the program
you want to run. Just put the file is a location you 
designate as its final home and point the dialog to that
location. Then follow the steps to tell Windows when and
how often to run the program.


Will this print the web page from the command line?
printdoc.vbs http://news.google.com

This question suggests to me that you didn't read the
comments included in the script, and it's very important
that you read them, as well as the comments I added.

In the script, that author notes:

'''''''''' Replace the web page with the one you want printed.
Set doc = wrd.Documents.Open("http://www.ericphelps.com/")

So you would need to change that line to the page you want
to print, as in:

Set doc = wrd.Documents.Open("http://news.google.com/")

Then you need no additional commands to run the script.
You can, indeed, run that from a command line.
Let's say you place the file as follows:

C:\batchfiles\printgoogle.vbs

You can just type that at a command line, and it will run.


Once you've satisfied yourself that the script works, by
running it with Word visible, and also giving Word a chance
to ask you if you want to update, if necessary, remember to:

'''''''''' Remove the below line if you don't want to watch Word 
wrd.Visible = True

...and to eliminate the MsgBox dialog, by commenting out:

If MsgBox ("This program illustrates how to automatically
print a web page by automating Microsoft Word. BUT -- it
will only work if you installed the HTML filters for Word
and you have an internet connection! OK to continue?",
vbOkCancel) = vbCancel Then Wscript.Quit


So your final, personalized script would look like this:

CODE

Dim wrd 'As Word.Application
Dim doc 'As Word.Document
Dim EndWait 'As Double

' If MsgBox ("This program illustrates how to automatically
' print a web page by automating Microsoft Word. BUT -- it
' will only work if you installed the HTML filters for Word
' and you have an internet connection! OK to continue?",
' vbOkCancel) = vbCancel Then Wscript.Quit

Set wrd = CreateObject("Word.Application")

'''''''''' Remove the below line if you don't want to watch Word 
' wrd.Visible = True

'''''''''' Replace the web page with the one you want printed.
Set doc = wrd.Documents.Open("http://news.google.com/")

'''''''''' This is a messy kluge to give the page time to load
'''''''''' If you try to print before the page is loaded, Word crashes!
EndWait = Now + (30 / 86400) ' 30 second delay to let page load
Do Until Now > EndWait
Loop

'''''''''' Un-comment the below line if you want to use a printer
'''''''''' other than your default. If you specify a printer,
'''''''''' it must be one that is already installed on your system.
'wrd.ActivePrinter = "HP LaserJet III"

doc.PrintOut
doc.Close
wrd.Quit
Set wrd = Nothing

/CODE

Save that with any name you like - printgoogle.vbs for example,
and place it where you like - C:\batchfiles\printgoogle.vbs
for example, and you're set to go.

Let me know if anything's not clear, or you have other questions.

sublime1-ga

Request for Answer Clarification by timtitus-ga on 01 Sep 2005 13:25 PDT
Sorry, but I was not clear with my request for revision:  The solution
should allow me to put the URL in the command line, like:

printweb.vbs http://news.google.com

Your solution does not offer this, so it's an incomplete solution. 
Can you re-code it so it can be called as previously requested?

Thanks!

Clarification of Answer by sublime1-ga on 01 Sep 2005 16:14 PDT
tim...

I apologize for the misunderstanding. In reading the original
question, it appeared that you simply wanted to be able to
regularly print a single webpage (Google News), and that it
wouldn't matter if the site address was specified in the code
or in the command line.

Is the problem that you want to be able to use the command on
such a large number of different webpages that the solution
of modifying the code to print each page isn't workable?

For example, if you're only going to use this on, say, five
different webpages, it would be relatively simple to modify
the code for each site and name the resulting file accordingly.

If, on the other hand, you expect to be using this to print
a large number of different websites, I can see why you'd be
reluctant to modify the code for that many sites.

If that's the case, I may have to ask the editors to remove
my answer and let someone else have a crack at the question,
though I doubt the specific solution you're seeking exists.

As for modifying this particular code so that the site address
goes in the command line, I don't think it can be done, since
this script necessarily performs a number of functions prior
to the address being entered, such as opening a Word document.

I can take another shot at finding an alternate script while
you respond to this post, but I'm not hopeful about there
being a solution which allows the address in the command line,
since there are several steps involved, and as mammens-ga
noted, those steps include downloading or opening the page
into some document prior to calling for the print process.
Thus I can't imagine any single script being able to do this
without first opening a document, and calling for the print
process later in the script.

Simply getting the site to open in a Word document, long 
before it can be printed, requires several lines of code
before the address can be specified:

Dim wrd 'As Word.Application
Dim doc 'As Word.Document
Dim EndWait 'As Double
Set wrd = CreateObject("Word.Application")
Set doc = wrd.Documents.Open("http://news.google.com/")

...which doesn't lend itself to the idea of having the
address in the command line itself. See what I mean?
The page *has* to be opened in a document of some kind
before it a print command can be given, and even that
precludes the use of the address in the command line.

Let me know what you think, in light of this.

sublime1-ga

Request for Answer Clarification by timtitus-ga on 01 Sep 2005 17:19 PDT
Is there a way to pay for 1/2 of the answer, and I'll be 100%
satisfied?  (I think I know someone who is a VB programmer who can
tell me how to "fix" this code to have the URL be an operand when the
script is run, and I'll give him the other half).

Clarification of Answer by sublime1-ga on 01 Sep 2005 17:47 PDT
tim...

Yes. You can change the price you've set on the question
by clicking on the Edit Question Parameters button, as 
illustrated in this GA User's Guide created by researcher
skermit-ga:
http://www.christopherwu.net/google_answers/answer_guide.html#changing_price

If your friend is successful I'd be curious to see the
final results, and I'm sure future GA readers would be
interested, as well.

Best regards...

sublime1-ga

Clarification of Answer by sublime1-ga on 01 Sep 2005 17:56 PDT
tim...

Oops...I wasn't thinking. You can only change the question
price prior to it being answered.

The alternative would be for me to ask the editors that this
answer be removed. At that point you would have the option of
changing the price, and I could repost the answer I gave you.

I can't guarantee you that they will accept this suggestion
from me, since it would essentially mean removing an answer
that you've already benefitted from, and the decision is
ultimately theirs to make. Since there's nothing to stop
you from cancelling the question without paying anything
after the answer is removed, they may be hesitant to do so.

All I can do is make the request. Let me know if that's how
you want to proceed.

sublime1-ga

Clarification of Answer by sublime1-ga on 01 Sep 2005 19:18 PDT
tim...

For what it's worth, it may ease your conscience to know that
researchers only receive 75% of the question price anyway.

sublime1-ga
timtitus-ga rated this answer:4 out of 5 stars
I should have worded the question better to get a more exact answer. 
Sublime did all he could do to try to help.

Comments  
Subject: Re: Need to find a program that can print a web page from a command-line
From: mammens-ga on 31 Aug 2005 10:24 PDT
 
http://www.bersoft.com/htmlprint/  -- has a utility that prints a html
page (check the trial version). Take a look. Seems to be just a
executable. See the licensing also. The catch is you need the page
downloaded as a .html file first to print the page.

There are lot of programs to get a web page saved as .html file. If
you achieve this, then you can give the .html file as parameter to the
htmlprint executable and print the page (ex; htmlprint
dailypage.html).
Subject: Re: Need to find a program that can print a web page from a command-line
From: mammens-ga on 07 Sep 2005 10:13 PDT
 
Tim,

The following code is a C# code to get a url saved as a.htm file. 
This takes care of just saving a file as .htm file only. 
Requirments:
.Net Framework to compile (to get the .exe out of this)

Once compiled you need to provide the URL as parameter to this exe.
eg: printweb http://news.google.com > test.htm 

and the output will be saved to test.htm
(I got this from the .net a while back and modified to take a
parameter,I do not remember now from where!!)

The other part is to have a utility to print the .htm file as is. 
As I noted earlier, htmlpint by bersoft can do this. Test and see.

Good luck

// C# Code to save url as a .html file
//-------------------------------------
using System;
using System.IO;
using System.Net;
using System.Text;

class GetPage
{
	static int Main(string[] args)
	{

                // Check if command-line arguments were supplied:
                if (args.Length == 0)  
                {
                   Console.WriteLine("Please enter a web page URL."); 
                   Console.WriteLine("Usage: printweb http://news.google.com>"); 
                   return 1; 
                }

		// Store the URL
		string urlStr = args[0];

		// used to build entire input
		StringBuilder sb  = new StringBuilder();

		// used on each read operation
		byte[]        buf = new byte[8192];

		// prepare the web page we will be asking for
		HttpWebRequest  request  = (HttpWebRequest)
			WebRequest.Create(urlStr);

		// execute the request
		HttpWebResponse response = (HttpWebResponse)
			request.GetResponse();

		// we will read data via the response stream
		Stream resStream = response.GetResponseStream();

		string tempString = null;
		int    count      = 0;

		do
		{
			// fill the buffer with data
			count = resStream.Read(buf, 0, buf.Length);

			// make sure we read some data
			if (count != 0)
			{
				// translate from bytes to ASCII text
				tempString = Encoding.ASCII.GetString(buf, 0, count);

				// continue building the string
				sb.Append(tempString);
			}
		}
		while (count > 0); // any more to read

		// print out page source
		Console.WriteLine(sb.ToString());

	}
        
}
// End

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy