mozdev.org

jsprintsetup

resources:

JSPrintSetup reference

JSPrintSetup is extension which give control to print settings through client side java script.

Extension give possibilities to web developers to control printing behavior of browser using client side java script.

Some of main features are:

This is a short reference about JSPrintSetup object which give control over browser printing features.

At first lets begin with sample code which demonstrate using of JSPrintSetup to setup print margins and call unattended print method (without print dialog).

<script>
   // set portrait orientation
   jsPrintSetup.setOption('orientation', jsPrintSetup.kPortraitOrientation);
   // set top margins in millimeters
   jsPrintSetup.setOption('marginTop', 15);
   jsPrintSetup.setOption('marginBottom', 15);
   jsPrintSetup.setOption('marginLeft', 20);
   jsPrintSetup.setOption('marginRight', 10);
   // set page header
   jsPrintSetup.setOption('headerStrLeft', 'My custom header');
   jsPrintSetup.setOption('headerStrCenter', '');
   jsPrintSetup.setOption('headerStrRight', '&PT');
   // set empty page footer
   jsPrintSetup.setOption('footerStrLeft', '');
   jsPrintSetup.setOption('footerStrCenter', '');
   jsPrintSetup.setOption('footerStrRight', '');
   // clears user preferences always silent print value
   // to enable using 'printSilent' option
   jsPrintSetup.clearSilentPrint();
   // Suppress print dialog (for this context only)
   jsPrintSetup.setOption('printSilent', 1);
   // Do Print 
   // When print is submitted it is executed asynchronous and
   // script flow continues after print independently of completetion of print process! 
   jsPrintSetup.print();
   // next commands
</script>

Reference of JSPrintSetup object

Contents

Methods

Common methods
Printing Options related methods
Enhanced Paper data handling
Printers handling
Print process control and information
Security Access Control

Constants

Methods

Common methods

wstring getVersion();

Returns jsPrintSetup version. Implemented from version 0.9.0.

void setOption( in string option, in wstring value);

Sets option value for currently selected printer. (See getPrinter and setPrinter methods)
List of possible options are described in the following table:

Option Description
orientation Paper orientation.
Available values are kPortraitOrientation and kLandscapeOrientation.
marginTop
marginBottom
marginLeft
marginRight
Top, Bottom, Left and Right print margins.
Margins by default are in millimeters.
Margins units can be changed with setPaperSizeUnit method.
The margins define the positioning of the content on the page. They're treated as an offset from the "unwriteable margin"
unwriteableMarginTop
unwriteableMarginLeft
unwriteableMarginBottom
unwriteableMarginRight
The unwriteable margin defines the printable region of the paper, creating an invisible border from which the edge and margin attributes are measured.
edgeTop
edgeLeft
edgeBottom
edgeRight
The edge measurements define the positioning of the headers and footers on the page. They're measured as an offset from the "unwriteable margin".
headerStrCenter
headerStrLeft
headerStrRight
Header strings at Left, Center and Right position.
Special strings can be used for:
&T – title
&U – URL
&D – date/time
&P – current page
&PT – total number of pages in form "*page* of *total*"
&L – last page number
footerStrCenter
footerStrLeft
footerStrRight
Footer strings at Left, Center and Right position.
Special strings can be used also as in header.
shrinkToFit Shring to fit to page. Available values 0 or 1.
numCopies Number of copies.
outputFormat Define Output Fiormat when printing to file. Available values are kOutputFormatNative, kOutputFormatPS, kOutputFormatPDF [FIXME]
toFileName Name and path of output file. See outputFormat [FIXME]
paperData Numeric value of paper size definition on Windows platform. See Windows Paper Sizes
paperName Name of the paper for Unix Platfom. It seems that is some like GtkPaperSize.
paperHeight Paper Height in getPaperSizeUnit units. Warning! This property don't follow right dimension of selected paper. It is better to use getPaperMeasure method and all paper handling methods.
paperWidth Paper Width in getPaperSizeUnit units. See comment on paperHeight.
paperSizeType On Windows platform control how to select paper from driver when print. There are two possible values kPaperSizeNativeData and kPaperSizeDefined. If you are using kPaperSizeNativeData you select paper with paperData option. If you are using kPaperSizeDefined then paper is matched to nearest values for paperHeight, paperWidth options. [FIXME] If you want to experiment with it, we recommend to use together with paper handling methods.
paperSizeUnit Paper size unit in which is stored by mozilla, not in units used by extension object. This option is read only and can’t be set.
scaling Scaling in percents.
shrinkToFit Boolean option. If true shrink size to be fit in page, else use scaling.
printRange What to print.Available values are:
kRangeAllPages – print all pages (by default)
kRangeSpecifiedPageRange – print specified pages (see startPageRange and endPageRange)
kRangeSelection – Prints selection
kRangeFocusFrame – print focused frame
startPageRange
endPageRange
In case of printRange = kRangeSpecifiedPageRange start and end print range.
printSilent Sets silent printing for current settings. Available values are 0 or 1.
Setting of this option is valid only when user preference 'print.always_print_silent' is not set.
When 'print.always_print_silent' is set, value of 'printSilent' option is ignored.
To clear 'print.always_print_silent' option you can use clearSilentPrint method.
printBGColors Turn On/Off printing of background colors. Available values are 0 or 1. (from version 0.8.2f)
printBGImages Turn On/Off printing of background images. Available values are 0 or 1. (from version 0.8.2f)

wstring getOption(in wstring option);

Returns value of selected option for current printer.
List of available options is described in setOption method.

void setGlobalOption( in string option, in wstring value);

wstring getGlobalOption(in wstring option);

Sets/Gets global printing option. List of available options is described in setOption method.

void setPaperSizeUnit(in short aPaperSizeUnit);

short getPaperSizeUnit();

Sets/Gets jsPrintSetup object Paper Size Units.
Available values are kPaperSizeMillimeters (by default) and kPaperSizeInches.

void saveOptions(in long optionSet);

Save currently selected printer options to user’s preferences in mozilla.
optionSet is bit set which control which settings to be saved.
List of available sets are described in self explanatory constants kSaveXXX.
For example kSaveAll save all options.

void saveGlobalOptions(in long optionSet);

Save global printing options to user’s preferences in mozilla.
optionSet is bit set which control which settings to be saved.
List of available sets are described in self explanatory constants kSaveXXX.
For example kSaveAll save all options.

void refreshOptions();

Reread global and printer settings (options) from users preferences saved in mozilla.

Enhanced Paper Handling Methods (introduced from version 0.9.0)

Because of different handling of paper information on different platforms, after detailed research was implemented methods which help to make easy to handle.

For example on windows is user paperData property which is Windows Paper Size number, but on Linux is used GtkPaperSize as paperName.

jsPrintSetup have build list of paper definitions which include this information for most used paper sizes, but also support method to add own paper definitions.

Every paper definition contain following information:

PD paperData See Windows Paper Size number
PN paperName -> paperName -> Linux(Unix) paperName. See GtkPaperSize
PWG Printing WorkGroup for Media Standartizied Names
Name Human readable Name
M Measure for Width and Heidth can be kPaperSizeInches or kPaperSizeMillimeters
W Width of paper in measure M
H Height of paper in measure M

jsPrintSetup builtin in paper definitons (first number is internal for jsPrinSetup paper ID)

  paperSizeList : {
      1 : {PD: 1, PN: 'na_letter',   PWG: 'na_letter_8.5x11in',       Name: 'US Letter', W: 8.5 , H: 11, M: kPaperSizeInches}
    , 2 : {PD: 2, PN: 'na_letter',   PWG: 'na_letter_8.5x11in',       Name: 'US Letter Small', W: 8.5 , H: 11, M: kPaperSizeInches} // pdf creator reports paper_data = 119!
    , 3 : {PD: 3, PN: 'ppd_Tabloid', PWG: 'na_ledger_11x17in',        Name: 'US Tabloid', W: 11 , H: 17, M: kPaperSizeInches}
    , 4 : {PD: 4, PN: 'ppd_Ledger',   PWG: '', /*???*/                 Name: 'US Ledger', W: 17 , H: 11, M: kPaperSizeInches}
    , 5 : {PD: 5, PN: 'na_legal',    PWG: 'na_legal_8.5x14in',        Name: 'US Legal', W: 8.5 , H: 14, M: kPaperSizeInches}
    , 6 : {PD: 6, PN: 'na_invoice',  PWG: 'na_invoice_5.5x8.5in',     Name: 'US Statement', W: 5.5 , H: 8.5, M: kPaperSizeInches}   // Half Letter
    , 7 : {PD: 7, PN: 'na_executive',PWG: 'na_executive_7.25x10.5in', Name: 'US Executive', W: 7.25 , H: 10.5, M: kPaperSizeInches}
    , 8 : {PD: 8, PN: 'iso_a3',      PWG: 'iso_a3_297x420mm',         Name: 'A3', W: 297 , H: 420, M: kPaperSizeMillimeters}
    , 9 : {PD: 9, PN: 'iso_a4',      PWG: 'iso_a4_210x297mm',         Name: 'A4', W: 210 , H: 297, M: kPaperSizeMillimeters}
    ,10 : {PD:10, PN: 'iso_a4',      PWG: 'iso_a4_210x297mm',         Name: 'A4 Small', W: 210 , H: 297, M: kPaperSizeMillimeters}
    ,11 : {PD:11, PN: 'iso_a5',      PWG: 'iso_a5_148x210mm',         Name: 'A5', W: 148 , H: 210, M: kPaperSizeMillimeters}
    ,12 : {PD:12, PN: 'jis_b4',      PWG: 'jis_b4_257x364mm',         Name: 'B4 (JIS)', W: 257 , H: 364, M: kPaperSizeMillimeters}
    ,13 : {PD:13, PN: 'jis_b5',      PWG: 'jis_b5_182x257mm',         Name: 'B5 (JIS)', W: 182 , H: 257, M: kPaperSizeMillimeters}
    ,14 : {PD:14, PN: 'om_folio',    PWG: 'om_folio_210x330mm',       Name: 'Folio', W: 210 , H: 330, M: kPaperSizeMillimeters}    // pdf creator FLSA
    ,15 : {PD:15, PN: 'na_quarto',   PWG: 'na_quarto_8.5x10.83in',    Name: 'Quarto', W: 8.5 , H: 10.83, M: kPaperSizeInches}
    ,16 : {PD:16, PN: 'na_10x14',    PWG: 'na_10x14_10x14in',         Name: '10x14 (envelope)', W: 10, H: 14, M: kPaperSizeInches}
    ,17 : {PD:17, PN: 'na_ledger',   PWG: 'na_ledger_11x17in',        Name: '11x17 (envelope)', W: 11, H: 17, M: kPaperSizeInches} // pdf creator Tabloid
    ,18 : {PD:18, PN: 'na_letter',   PWG: 'na_letter_8.5x11in',       Name: 'US Note', W: 8.5 , H: 11, M: kPaperSizeInches} // == letter
    ,19 : {PD:19, PN: 'na_number-9', PWG: 'na_number-9_3.875x8.875in',Name: 'US Envelope #9', W: 3.875, H: 8.875, M: kPaperSizeInches}
    ,20 : {PD:20, PN: 'na_number-10',PWG: 'na_number-10_4.125x9.5in', Name: 'US Envelope #10', W: 4.125, H: 9.5, M: kPaperSizeInches}
    ,21 : {PD:21, PN: 'na_number-11',PWG: 'na_number-11_4.5x10.375in',Name: 'US Envelope #11', W: 4.5, H: 10.375, M: kPaperSizeInches}
    ,22 : {PD:22, PN: 'na_number-12',PWG: 'na_number-12_4.75x11in',   Name: 'US Envelope #12', W: 4.75, H: 11, M: kPaperSizeInches}
    ,23 : {PD:23, PN: 'na_number-14',PWG: 'na_number-14_5x11.5in',    Name: 'US Envelope #14', W: 5, H: 11.5, M: kPaperSizeInches}
    ,24 : {PD:24, PN: 'na_c',        PWG: 'na_c_17x22in',             Name: 'C size sheet', W: 17, H: 22, M: kPaperSizeInches}
    ,25 : {PD:25, PN: 'na_d',        PWG: 'na_d_22x34in',             Name: 'D size sheet', W: 22, H: 34, M: kPaperSizeInches}
    ,26 : {PD:26, PN: 'na_e',        PWG: 'na_e_34x44in',             Name: 'E size sheet', W: 34, H: 44, M: kPaperSizeInches}
    ,27 : {PD:27, PN: 'iso_dl',      PWG: 'iso_dl_110x220mm',         Name: 'Envelope DL', W: 110, H: 220, M: kPaperSizeMillimeters}
    ,28 : {PD:28, PN: 'iso_c5',      PWG: 'iso_c5_162x229mm',         Name: 'Envelope C5', W: 162, H: 229, M: kPaperSizeMillimeters}
    ,29 : {PD:29, PN: 'iso_c3',      PWG: 'iso_c3_324x458mm',         Name: 'Envelope C3', W: 324, H: 458, M: kPaperSizeMillimeters}
    ,30 : {PD:30, PN: 'iso_c4',      PWG: 'iso_c4_229x324mm',         Name: 'Envelope C4', W: 229, H: 324, M: kPaperSizeMillimeters}
    ,31 : {PD:31, PN: 'iso_c6',      PWG: 'iso_c6_114x162mm',         Name: 'Envelope C6', W: 114, H: 162, M: kPaperSizeMillimeters}
    ,32 : {PD:32, PN: 'iso_c6c5',    PWG: 'iso_c6c5_114x229mm',       Name: 'Envelope C65', W: 114, H: 229, M: kPaperSizeMillimeters}
    ,33 : {PD:33, PN: 'iso_b4',      PWG: 'iso_b4_250x353mm',         Name: 'Envelope B4', W: 250, H: 353, M: kPaperSizeMillimeters}
    ,34 : {PD:34, PN: 'iso_b5',      PWG: 'iso_b5_176x250mm',         Name: 'Envelope B5', W: 176, H: 250, M: kPaperSizeMillimeters}
    ,35 : {PD:35, PN: 'iso_b6',      PWG: 'iso_b6_125x176mm',         Name: 'Envelope B6', W: 125, H: 176, M: kPaperSizeMillimeters}
    ,36 : {PD:36, PN: 'om_italian',  PWG: 'om_italian_110x230mm',     Name: 'Italian Envelope', W: 110, H: 230, M: kPaperSizeMillimeters}
    ,37 : {PD:37, PN: 'na_monarch',  PWG: 'na_monarch_3.875x7.5in',   Name: 'US Envelope Monarch', W: 3.875, H: 7.5, M: kPaperSizeInches}
    ,38 : {PD:38, PN: 'na_personal', PWG: 'na_personal_3.625x6.5in',  Name: 'US Personal Envelope', W: 3.625, H: 6.5, M: kPaperSizeInches} // 6 3/4 Envelope
    ,39 : {PD:39, PN: 'na_fanfold-us',PWG:'na_fanfold-us_11x14.875in',Name: 'US Std Fanfold', W: 11, H: 14.875, M: kPaperSizeInches}
    ,40 : {PD:40, PN: 'na_fanfold-eur',PWG:'na_fanfold-eur_8.5x12in', Name: 'German Std Fanfold', W: 8.5, H: 12, M: kPaperSizeInches}
    ,41 : {PD:41, PN: 'na_foolscap', PWG:'na_foolscap_8.5x13in',      Name: 'German Legal Fanfold', W: 8.5, H: 13, M: kPaperSizeInches}
    // 42 = ISO B4? === 33 by paper size
    ,43 : {PD:43, PN: 'jpn_hagaki',  PWG:'jpn_hagaki_100x148mm',      Name: 'Japanese Postcard', W: 100, H: 148, M: kPaperSizeMillimeters}
  },

wstring getPaperSizeList(); (from ver.0.9.0)

Return JSON string of all paper definitions.

void definePaperSize(in short jspid, in short pd, in wstring pn, in wstring pwg, in wstring name, in double w, in double h, in short m); (from ver.0.9.0)

Define paper data. All parameters correspon of paper definition data but in small letters. jspid is internal jsPrintSetup paper ID.

void undefinePaperSize(in short jspid); (from ver.0.9.0)

Remove paper definition with jsPrintSetup paper ID.

wstring getPaperSizeDataByID(in short jspid); (from ver.0.9.0)

Returns JSON string with paper definition for paper with given jsPrintSetup paper ID.

wstring getPaperSizeData(); (from ver.0.9.0)

Returns JSON string with paper definition for currently used paper.

void setPaperSizeData(in short jspid); (from ver.0.9.0)

Set current paper to be used for print. Paper is select by jsPrintSetup paper ID.

wstring getPaperMeasure(); (from ver.0.9.0)

Returns paper measures as JSON string for currently selected paper.
Object contain Width and Height of paper in paperSizeUnits measure. If paper exact match of some paper definition it is returned, else us used paperHeight and paperWidth options.

Printers Handling

wstring getPrintersList();

Returns comma separated list of available printers. There is known problems on Mac OSX with this method see Bug 22369.

void setPrinter(in wstring aPrinterName);

Set current printer.

wstring getPrinter();

Returns currently selected printer.

void print();

Prints content of most recent browser window to currently selected printer with current settings like window.print method.
The difference between window.print() and jsPrintSetup.print() is that window.print() prints using settings saved in user preferences, while jsPrintSetup.print() print with current printer settings stored in jsPrintSetup.

void printWindow(in nsIDOMWindow aWindow);

This method is similar to print() method but have a parameter which pass window (frame to print).
I will illustrate it with example:
If you have main.html which include frame.html whats happen:
--- frame.html ----
....
<script>
function test() {
   jsPrintSetup.setPrinter('PDFCreator');
   jsPrintSetup.setSilentPrint(1);
   jsPrintSetup.setOption('headerStrCenter', 'bla bla frame');
   jsPrintSetup.setOption('headerStrRight', '');
   // here window is current frame!
   jsPrintSetup.printWindow(window);
   jsPrintSetup.setSilentPrint(0);
}
</script>
 </head>
 <body>
<a href="javascript: test();">Test</a>
 </body>
</html>

--- main.html ----
<script>
function test1() {
   jsPrintSetup.setPrinter('PDFCreator');
   jsPrintSetup.setSilentPrint(1);
   jsPrintSetup.setOption('headerStrCenter', 'bla bla main');
   jsPrintSetup.setOption('headerStrRight', '');
// print current window with all frames in it
   jsPrintSetup.printWindow(window);
// print desired frame
//    jsPrintSetup.printWindow(window.frames[0]);
   jsPrintSetup.setSilentPrint(0);
}
</script>
 </head>
 <body>
<iframe name="test" src="frame.html"></iframe>
<a href="javascript: test1();">Test</a>
 </body>
</html>

void setSilentPrint(in boolean flag);

Turn On/Off print dialog through manipulating user preferences setting 'print.always_print_silent'. This method manipulate setting stored in user’s preferences and have global sense for mozilla, i.e. this behavior is for all tabs and windows opened in mozilla and will be active until next change or manual edit in about:config.
Recommended scenario of usage will be:
<script>
   var oldSilentPrint = jsPrintSetup.getSilentPrint();
   jsPrintSetup.setSilentPrint(true);
   jsPrintSetup.print();
   jsPrintSetup.setSilentPrint(oldSilentPrint);
</script>
Important Note! Because print method is executed asynchronously when setSilentPrint(false) is called imediately after print method, dialog may be appear (see bug 21473). To workaround this you can use 'printSilent' option instead, which is take in care only when user preference 'print.always_print_silent' is not set. Then scenario will be the following:
<script>
   jsPrintSetup.clearSilentPrint();
   jsPrintSetup.setOption('printSilent', 1);
   jsPrintSetup.print();
</script>

boolean getSilentPrint();

Returns current value for SilentPrint. (See setSilentPrint)

void clearSilentPrint();

(added from version 0.8.2e)
Clears the value of 'print.always_print_silent' setting stored in user’s preferences.
This method is different from setSilentPrint(false), see notes and example for setSilentPrint method about 'printSilent' option.

void setShowPrintProgress(in boolean flag);

Turn On/Off printing progress window.
This method manipulate setting stored in user’s preferences and have global sense for mozilla, i.e. this behavior is for all tabs and windows opened in mozilla and will be active until next change or manual edit in about:config.
Recommended scenario of usage will be:
<script>
   var oldShowPrintProgress = jsPrintSetup. getShowPrintProgress();
   jsPrintSetup. setShowPrintProgress(false);
   jsPrintSetup.print();
   jsPrintSetup. setShowPrintProgress(oldShowPrintProgress);
</script>

boolean getShowPrintProgress();

Returns current value for ShowPrintProgress. (See setShowPrintProgress)

void setPrintProgressListener(in nsIWebProgressListener listener); (available from ver. 0.8.2g)

(Special thanks to Josh Stutts for implementing this method)
This method can set custom print progress listener function.
Method accept as parameter object that supports nsIWebProgressListener interface.
About nsIWebProgressListener interface can read more at http://developer.mozilla.org/en/nsIWebProgressListener or http://www.oxymoronical.com/experiments/xpcomref/applications/Firefox/3.5/interfaces/nsIWebProgressListener.

Example:
<script>
// define progress listener object
var progressListener = {
   stateIsRequest:false,
   QueryInterface : function(aIID) {
     if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
         aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
         aIID.equals(Components.interfaces.nsISupports))
         return this;
     throw Components.results.NS_NOINTERFACE;
   },
   
   onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {
     alert('State Change -> State Flags:'+aStateFlags+' Status:'+aStatus);
     return 0;
   },
   
   onLocationChange : function(aWebProgress, aRequest, aLocation) {
     return 0;
   },
   
   onProgressChange : function(aWebProgress, aRequest,
     aCurSelfProgress, aMaxSelfProgress,
     aCurTotalProgress, aMaxTotalProgress){
     alert('Self Current:'+aCurSelfProgress+' Self Max:'+aMaxSelfProgress
             +' Total Current:'+aCurTotalProgress+' Total Max:'+aMaxTotalProgress);
   },
   
   onStatusChange : function(aWebProgress, aRequest, aStateFlags, aStatus) {
     alert('Status Change -> State Flags:'+aStateFlags+' Status:'+aStatus);
   },
   onSecurityChange : function(aWebProgress, aRequest, aState){}
   //    onLinkIconAvailable : function(a){}
};


function test() {
   // sets printer 
   jsPrintSetup.setPrinter('PDFCreator');
   // sets print progress object
   jsPrintSetup.setPrintProgressListener(progressListener);
   // sets silent printing
   jsPrintSetup.setSilentPrint(true);
   // do print
   jsPrintSetup.print();
   // restore silent printing
   jsPrintSetup.setSilentPrint(false);
}
</script>

Security Access Control (Important New feature implemented from ver. 0.9.0)

Important Note!
If your application is not conformable with jsPrintSetup access control, most of features of jsPrintSetup will be inaccessible if user who is using application block access to jsPrintSetup on first request for permission.
Your application must implement these methods to get information about user's decision and rerequest permissions if needed or get alternate decision.

It is very important to control who can use jsPrintSetup to manipulate your printer settings.
jsPrintSetup implements Firefox permission manager to controls host access to jsPrintSetup. List of Allowed and Blocked hosts.
Here is right point to mention that these features are realised with valuable help of Guillaume Crico.
jsPrintSetup have implemented some new options (addon options dialog) and methods for programmers about security.
jsPrintSetup Security options
Security Mode There are three options:
- Enable access from all sites. Equal of that no restrictions
- Enable access ONLY from allowed sites. Blocked sites are silently denied.
- Automatic Prompt for permission if host is not Allowed or Blocked.(By default)
Enable access from local files Allow/Block access from local files is treated by different way. They are not controlled from permission manager (this is FF permission manager speciality). By default is enabled.
Enable rerequest permissions from blocked sites Is site is in Black list this enable calling of jsPrintSetup.askUserPermissions. By default is enabled.

Security Methods

PRUint32 getPermissions();

Returns current permissions over jsPrintSetup. See permisions constants JSPS_ALLOW_ACTION, JSPS_DENY_ACTION, JSPS_UNKNOWN_ACTION.

void askUserPermissions(in jsPrintSetupCallback callback);

Initiate jsPrintSetup to request access from user. It accepts optional callback object (or null) which is called on user decision (Alock/Allow).

void setCallback(in jsPrintSetupCallback callback);

Set jsPrintSetup callback object which at this moment implements only two method related to security.
permissionsCheck(in boolean enabled) which is called with result of internal for jsPrintSetup check of permissions.
permissionsAsk(in boolean allowed) which is called after user give decision of request for permission.

jsPrintSetup callback definition

interface jsPrintSetupCallback : nsISupports
{
   void permissionsCheck(in boolean enabled);
   void permissionsAsk(in boolean allowed);
}; 

Sample Example

<script>

var jsPrintSetupCallbackObject = {
   permissionsCheck: function(enabled) {
      alert('permissionsCheck:'+(enabled?'enabled':'disabled'));
   },
   permissionsAsk: function(allowed) {
      alert('permissionsAsk:'+(allowed?'allowed':'blocked'));
   }
}

function checkUseJSPrintSetup()
{
   jsPrintSetup.setCallback(jsPrintSetupCallbackObject);
   var perm = jsPrintSetup.getPermissions();
   if (perm == jsPrintSetup.JSPS_DENY_ACTION) {
      // if you don't want to set callback pass null
      jsPrintSetup.askUserPermissions(null);
   } // else jsPrintSetup will request automatically
   // requiest arbitrary method from jsPrintSetup which require permission 
   this.alert(jsPrintSetup.getPrinter());
}
</script>

Constants

Constants are displayed in self explanatory as defined in IDL file.
   // Save Options Constants   
   const unsigned long kSaveOddEvenPages = nsIPrintSettings::kInitSaveOddEvenPages; 
   const unsigned long kSaveHeaderLeft = nsIPrintSettings::kInitSaveHeaderLeft;
   const unsigned long kSaveHeaderCenter = nsIPrintSettings::kInitSaveHeaderCenter;
   const unsigned long kSaveHeaderRight = nsIPrintSettings::kInitSaveHeaderRight;
   const unsigned long kSaveFooterLeft = nsIPrintSettings::kInitSaveFooterLeft;
   const unsigned long kSaveFooterCenter = nsIPrintSettings::kInitSaveFooterCenter;
   const unsigned long kSaveFooterRight = nsIPrintSettings::kInitSaveFooterRight;
   const unsigned long kSaveBGColors  = nsIPrintSettings::kInitSaveBGColors ;
   const unsigned long kSaveBGImages = nsIPrintSettings::kInitSaveBGImages;
   const unsigned long kSavePaperSize = nsIPrintSettings::kInitSavePaperSize;
   const unsigned long kSavePaperData = nsIPrintSettings::kInitSavePaperData;
   const unsigned long kSavePaperSizeNativeData = nsIPrintSettings:: kPaperSizeNativeData;
   const unsigned long kSavePaperWidth = nsIPrintSettings::kInitSaveUnwriteableMargins;
   const unsigned long kSavePaperHeight = nsIPrintSettings::kInitSaveEdges;
   const unsigned long kSaveInColor = nsIPrintSettings::kInitSaveInColor;
   const unsigned long kSaveOrientation = nsIPrintSettings::kInitSaveOrientation;
   const unsigned long kSavePrinterName = nsIPrintSettings::kInitSavePrinterName;
   const unsigned long kSavePageDelay = nsIPrintSettings::kInitSavePageDelay;
   const unsigned long kSaveMargins = nsIPrintSettings::kInitSaveMargins;
   const unsigned long kSaveShrinkToFit = nsIPrintSettings::kInitSaveShrinkToFit;
   const unsigned long kSaveResolutionName = nsIPrintSettings::kInitSaveResolutionName;
   
   const unsigned long  kSaveScaling = nsIPrintSettings:: kInitSaveScaling;

   // Page Size Unit Constants 
   const short kPaperSizeInches      = nsIPrintSettings::kPaperSizeInches;
   const short kPaperSizeMillimeters = nsIPrintSettings::kPaperSizeMillimeters;

   // Page Orientation Constants
   const short kPortraitOrientation  = nsIPrintSettings::kPortraitOrientation;
   const short kLandscapeOrientation = nsIPrintSettings::kLandscapeOrientation;

   // Paper Size Data Constants
   const short kPaperSizeNativeData  = nsIPrintSettings::kPaperSizeNativeData;
   const short kPaperSizeDefined     = nsIPrintSettings::kPaperSizeDefined;
   
   // Print Range Enums 
   const long kRangeAllPages           = nsIPrintSettings::kRangeAllPages;
   const long kRangeSpecifiedPageRange = nsIPrintSettings::kRangeSpecifiedPageRange;
   const long kRangeSelection          = nsIPrintSettings::kRangeSelection;
   const long kRangeFocusFrame         = nsIPrintSettings::kRangeFocusFrame;

   // Output format
   const short kOutputFormatNative = nsIPrintSettings::kOutputFormatNative;
   const short kOutputFormatPS = nsIPrintSettings::kOutputFormatPS;
   const short kOutputFormatPDF = nsIPrintSettings::kOutputFormatPDF;
   
   // Shorthand Combined Saving Constants
   const unsigned long kSaveHeader = 
                                kSaveHeaderLeft | kSaveHeaderCenter | kSaveHeaderRight | 
                                kSaveFooterLeft | kSaveFooterCenter | kSaveFooterRight;
   const unsigned long kSaveMarginsAndOrientation = 
                                    kSaveOrientation | kSaveMargins;
   const unsigned long kSaveMarginsAndHeader =
                                 kSaveHeader | kSaveMarginsAndOrientation;
   const unsigned long kSaveAll = 
                                kSaveOddEvenPages 
                              | kSaveHeaderLeft |kSaveHeaderCenter | kSaveHeaderRight  
                              | kSaveFooterLeft | kSaveFooterCenter | kSaveFooterRight 
                              | kSaveBGColors | kSaveBGImages 
                              | kSavePaperSize | kSavePaperData | kSavePaperSizeNativeData 
                              | kSavePaperWidth | kSavePaperHeight | kSaveInColor 
                              | kSaveOrientation |   kSavePrinterName | kSavePageDelay 
                              | kSaveMargins |   kSaveShrinkToFit | kSaveScaling 
                              | kSaveResolutionName;

   // Permissions constants                              
   const PRUint32 JSPS_ALLOW_ACTION = nsIPermissionManager::ALLOW_ACTION;
   const PRUint32 JSPS_DENY_ACTION = nsIPermissionManager::DENY_ACTION;
   const PRUint32 JSPS_UNKNOWN_ACTION = nsIPermissionManager::UNKNOWN_ACTION;

The jsprintsetup project can be contacted through the mailing list or the member list.
Copyright © 2000-2017. All rights reserved. Terms of Use & Privacy Policy.