Shanky_Richer
Shanky_Richer

Reputation: 223

How to pass printing range of pages in javascript using window.print()?

I am working on a HTML and javascript project. I want to print my document using javascript. I am using window.print() to print. But how can i pass printing range also? eg: If a document contains 20 pages and i want to print pages from 5-10 only.

Upvotes: 5

Views: 4573

Answers (5)

Amir M. Mohamadi
Amir M. Mohamadi

Reputation: 1522

I use print @media rule to check if it is print or not and then hide certain elements based on that:

@media print {
  .hide-print {
    display: none !important;
  }  
}

Upvotes: 0

CSᵠ
CSᵠ

Reputation: 10169

window.print() does not accept any arguments,
but you could make use of the: window.onbeforeprint event for your case.

Just toggle display: none for the sections in your page you do not wish to be printed.

Upvotes: 2

Mr. Alien
Mr. Alien

Reputation: 157404

You cannot set a range using window.print(), using window.print() invokes your print preferences window which is native print window, where you can set the page range manually..

Perhaps you can dynamically set a class for the content you don't want to get printed, using print specific stylesheet or media queries.

Upvotes: 3

pvanhouten
pvanhouten

Reputation: 782

You have the option of installing an ActiveX control (perhaps if this is for an internal application) that would expose those values to you through javascript. I've used MeadCo ScriptX for this in the past and it's worked out quite nicely.

Upvotes: 0

lukad
lukad

Reputation: 17873

You can't do that. All window.print() can do is open the operating system's default print dialog.

But maybe you could do something like hiding those parts of site that shouln't be printed.

Upvotes: 0

Related Questions