• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 40956
  • Last Modified:

Printing from a website (ASP & JavaScript)

Hi,

I have an ASP page that draws up a report and display it to the user. If the user wishes to do so, he can click a "print" button which activates the printer dialog via javascript. This would be the same as if they selected "File > Print" from the browser menu.
My problem is that at hte top of the page, page numbers are added automatically, and at the bottom the URL of the page that was printed. Is there a way to get rid of this extra information, so that a "clean" document can be printed with only the results from the report.

Any help would be appreciated,
Thanks.
0
split-etm-
Asked:
split-etm-
1 Solution
 
pkaledaCommented:
Not printing via a browser.  You can give them a way to open the report in Excel via a CSV file or XLS file.  They can then print from there.

PK
0
 
eeBlueShadowCommented:
These details are added in the browser preferences (File->Page Setup in IE) and you can't change it using web scripting. Sorry :(

_Blue
0
 
COBOLdinosaurCommented:
If you are going across the Internet with this you best option is to give the user instructions of how to change their settings as outlined by eeBlueShadow.

If this is on an intranet you can use activeX to modify the settings programatically using:

http://www.meadroid.com/scriptx/index.asp

I recommend against using the ActiveX solution across the Internet because it is IE only; may be blocked by some corporate firewalls, may generate a security alert and the user will have to agree to allow it.

Cd&
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Sleepyhead_NOCommented:
it is not possible to change the print settings used by IE using normal html and javascript. You can trigger the print event by using window.print(), but you cannot set the configuration when printing. Only alternative is to either print via Microsoft Word (set ContentType to word document and alter the document to what you want to output) or use an activeX script - but that will require the user to allow activeX scripts to execute (put your domain in the trusted domain list or change the IE settings to allow active X).
0
 
emblueCommented:
I've used the ScriptX activeX script that Coboldinosaur has refered to, and it works great.

Yes, IE users will be prompted with a security alert, but if that works for you this is an ideal solution.  If you download the code from the link http://www.meadroid.com/scriptx/sxdownload.asp   extract the zip file and upload ScriptX.cab to your web space.  (Be sure to use Binary FTP mode if you are on windows)


Here is some example code to make it work, you just stick this right into your HTML output:

<HTML>

<HEAD>

<!--- JavaScript to operate the activeX object -->
<script language="Javascript"><!--
function doprint() {
  //save existing user's info
  var h = factory.printing.header;
  var f = factory.printing.footer;
  //hide the button
  document.all("printbtn").style.visibility = 'hidden';
  //set header and footer to blank
  factory.printing.header = "";
  factory.printing.footer = "";
  //print page without prompt
  factory.DoPrint(false);
  //restore user's info
  factory.printing.header = h;
  factory.printing.footer = f;
  //show the print button
  document.all("printbtn").style.visibility = 'visible';
}
//--></script>

</HEAD>

<BODY>



<object id="factory" viewastext  style="display:none"
  classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"
  codebase="./ScriptX.cab#Version=6,1,431,2">
</object>
<input type="button" name="printbtn" onClick="doprint()" value="Print">



</BODY>
</HTML>



The above code will create a button with the text "Print" if the user clicks it, the printout will have no header/footer text added by the browser.  The user will be prompted to install the activeX by a security dialogue if their security settings are set to medium or high on internet explorer.  However, they will only need to click yes the first time they visit your page.



Emblue


0
 
emblueCommented:
The author of this post has not commented for quite some time.  Can we please get an update on the issue, or close the question?

Emblue
0
 
split-etm-Author Commented:
Hi Guys,

Sorry yes i have not come back to give feedback on this question. i did find a solution with the help from eeBlueShadow, but i had to add some script to make it work the way i wanted. Thanks eeBlueShadow.
Thanks to all the rest as well for contributions, unfortunately i could not use the activex scripting solution, even though im sure it would have worked, because of some security/compatibility issues.

i am not sure if anyone would be interested, but here is what i did to solve my problem. after removing the header/footer, i added the following code to display a 'close' button, but without actually printing it on the reports...i needed this because the report window opens in a chromeless window, which does not display any standard window controls. i added this code to the document for the button:

<div id="divHide" name="divHide" style="visibility:hidden;">
      <font style="font-family:Verdana;font-size:7pt"><a href="#" onclick="javascript:window.close();">Close</a></font>
</div>

<script language=javascript>
      window.print();
      
      function window.onafterprint() {
            //after printing is done, set the close button
            document.all.divHide.style.visibility = 'visible';
      }
</script>

the button will at first be hidden, until after the document displayed the printing dialog. by using the window.onafterprint() event, i could enable the button's visibility, which would enable the user to close the window.

cheers & thanks again!
0
 
mbwatkinsCommented:
how can you set the lenght of the footer using framing object (active x)?
The text I am triyng to set is too long
thanks
0
 
mvnoppenCommented:
I found this script somewhere on the net and it works (though you get a warning about unsafe ActiveX)
you should call ResetHeader() to restore the settings.

<script Language="vbscript">
    Dim WSHShell
    Dim myHeader
    Dim myFooter
    Set WSHShell = CreateObject("WScript.Shell")
    myHeader = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\header")
    myFooter = WSHShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer")

    WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", ""
    WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", ""

    Sub ResetHeader()
        Dim WSHShell
        Set WSHShell = CreateObject("WScript.Shell")
        WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\header", myHeader
        WSHShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\PageSetup\footer", myFooter
    End Sub
</script>      
0
 
yojan_aguilarCommented:
I got an error here
CreateObject("WScript.Shell")
(The ActiveX component cannot create the object WScript.Shell)

Do I have to download and register it  (Does it coast ?)

Many Thanks
AAP
0
 
HackerNeoCommented:
I got an error here
CreateObject("WScript.Shell")
(The ActiveX component cannot create the object WScript.Shell)

Do I have to download and register it  (Does it coast ?)

I think you have the wrong idea here, but can I firstly ask for hw you are trying sto use this, if you are using ASP you will need to use

Server.CreateObject("WScript.Shell")

HTH
0
 
SuperGhostyCommented:
You may not have the WSH installed (Windows Scripting Host) -
Visit the official site at: http://msdn.microsoft.com/scripting/ and you can download it by clicking on "Script Downloads"

This type of scripting is generally used with an intranet because Internet Explorer will ask you if you want to run the ActiveX script, etc... and most users will not run it + security risk, etc.... If you use this on an intranet site it will usually run without any problems (depending on your security settings).

HackerNeo, Since this is a client-side script (vbscript is client-side ASP is server-side) you do not need the Server.CreateObject, it is correct the way it is written. WSH is free and a simple install takes care of everything.

Also in the Registry under: HKCU\Software\Microsoft\Internet Explorer\PageSetup\
You will also find keys for the printer margins along with the header and footer (listed below)

header, footer, margin_top, margin_bottom, margin_left, margin_right

Hope this helps, I used this same script for an intranet app that had to modify the margins to print to a label printer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now