Solved

Printing from a website (ASP & JavaScript)

Posted on 2004-04-21
12
40,837 Views
Last Modified: 2011-08-18
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
Comment
Question by:split-etm-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 6

Expert Comment

by:pkaleda
ID: 10878299
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
 
LVL 10

Accepted Solution

by:
eeBlueShadow earned 250 total points
ID: 10878394
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
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 10881798
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 3

Expert Comment

by:Sleepyhead_NO
ID: 10910063
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
 
LVL 4

Expert Comment

by:emblue
ID: 10964891
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
 
LVL 4

Expert Comment

by:emblue
ID: 11271101
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
 

Author Comment

by:split-etm-
ID: 11303987
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
 

Expert Comment

by:mbwatkins
ID: 11754833
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
 

Expert Comment

by:mvnoppen
ID: 13401199
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
 

Expert Comment

by:yojan_aguilar
ID: 13636728
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
 

Expert Comment

by:HackerNeo
ID: 14473568
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
 
LVL 3

Expert Comment

by:SuperGhosty
ID: 14486654
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Matthew
I am a very big proponent of technology compliance standards and strive to meet such criteria in all of my work. That includes my site, which is 100% XHTML 1.0 compliant as determined by the World Wide Web Consortium. https://www.matthewstevenkel…
It's sometimes a bit tricky to use date functions in Oracle BPEL. I'll explain quickly how you can add N days to the current date. In a BPEL process this can be useful, and you can adapt it to fit your needs. First of all, let's see how to add 1 …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question