Forum Moderators: open

Message Too Old, No Replies

Print Page Range

ie

         

aspdaddy

10:49 am on Jun 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does anyone know the code to automate the print dialoge and send a page range to it, ideally click the print button as well.

Thanks.

subexpression

5:06 am on Jun 8, 2010 (gmt 0)

10+ Year Member



aspdaddy,

Javascript's window.print() doesn't actually accept any parameters in the argument list...and cannot do what you're asking.

ActiveX can certainly pull it off, but you'd be prompting your users to install the plugin first, which quite frankly, scares the pants off most people.

However, you can control "what" gets printed several ways.
  • control the page media with CSS
  • send content to a js function which opens a new window, and initiates the window.print() function


With CSS, you can specify which elements are visible on the web page (screen) or the elements which get printed (print):


@media screen {
body { font-size: 13px }
}
@media print {
body { font-size: 10pt }
}


Both "screen" and "print" can have shared rules:

@media screen, print {
body { line-height: 1.2 }
}


With Javascript, you could try printing with parameters with a custom method:

<script type="text/javascript">
function customPrint(content) {
var win = window.open();
self.focus();
win.document.open();
win.document.write('<'+'html'+'><'+'body'+'>');
win.document.write(content);
win.document.write('<'+'/body'+'><'+'/html'+'>');
win.document.close();
win.print();
win.close();
}
</script>

Then, you can pass the content you want to print as a parameter:

<input type="button" onclick="customPrint('text text text');" value="Print" />


A more sophisticated approach would be to set up an XMLHttpRequest (AJAX) object, send the responseText to the customPrint() function as the content you wish to print.

None of these will automate the print dialog. For security reasons, the print dialog only prints when the user clicks the "Print" button on the browser's print dialog.
In the same way, Javascript will not allow you to create click or key events since this would be a serious security vulnerability. You can capture click, key and other events, but you can't create them. It must be user-initiated.

aspdaddy

1:55 pm on Jun 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the info sub. I have used the CSS stuff in the past but its a nightmare to maintain.

I looked into the component though and found a nice solution , the interface is very simple and it worked first time!



<script type="text/javascript" language="jscript">
function PrintPages(blnPrompt, intStartPage, intEndPage) {
with (factory.printing){
SetPageRange(false,intStartPage,intEndPage);
Print(blnPrompt);
WaitForSpoolingComplete();
}}
</script>

<!-- Print page range 1 to 10 with no prompt -->
<button onclick="PrintPages(false,1,10)">Print Pages 1-10</button>

subexpression

4:15 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



If you can guarantee all your users are going to have Internet Explorer, such as a corporate environment, then ScriptX might be a viable solution. My company uses several plugins which only work in IE.

I find it humorous that on their websiste it says:
ScriptX: now the de facto printing control for the web

I'm not sure how it's de facto if it only works in Internet Explorer!

Personally, I never use IE except for cross-browser code testing. Contingency planning ;)