Solved

Printing from a website (ASP & JavaScript)

Posted on 2004-04-21
12
40,782 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-
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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to count occurrences of each item in an array.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now