Closed Bug 545839 Opened 14 years ago Closed 14 years ago

ReferenceError: urls is not defined Error while working withn XUL

Categories

(Mozilla Localizations :: mr / Marathi, defect)

x86
Linux
defect
Not set
normal

Tracking

(Not tracked)

RESOLVED INVALID

People

(Reporter: kasturikarlekar, Unassigned)

Details

User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5) Gecko/2008032600 SUSE/2.9.95-25.1 Firefox/3.0b5
Build Identifier: 

I am pasting my code here,

/*
  Convert HTML to PDF with mozilla rendering engine.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  USA

  Michele Baldessari <michele@pupazzo.org> 2006
  Huang Ying <huang.ying.caritas@gmail.com> 2008
*/

function WebProgressListener() {
    var self = this;
}

WebProgressListener.prototype.
QueryInterface = function(iid) {
    var self = this;
    if (iid.equals(Components.interfaces.nsIWebProgressListener) ||
	iid.equals(Components.interfaces.nsISupportsWeakReference) ||
	iid.equals(Components.interfaces.nsISupports))
	return self;

    throw Components.results.NS_ERROR_NO_INTERFACE;
};

WebProgressListener.prototype.
onStateChange = function(webProgress, request, stateFlags, status) {
    var self = this;
    const WPL = Components.interfaces.nsIWebProgressListener;

    if (stateFlags & WPL.STATE_STOP) {
	if ("onDone" in self)
	    self.onDone();
	if (stateFlags & WPL.STATE_IS_NETWORK)
	    if ("onNetworkDone" in self)
		self.onNetworkDone();
    }
};

WebProgressListener.prototype.
onProgressChange = function(webProgress, request, curSelf, maxSelf,
			    curTotal, maxTotal) { };

WebProgressListener.prototype.
onLocationChange = function(webProgress, request, location) { };

WebProgressListener.prototype.
onStatusChange = function(webProgress, request, status, message) { };

WebProgressListener.prototype.
onSecurityChange = function(webProgress, request, state) { };

function HtmlPrinter() {
    var self = this;
    self.browser = document.getElementById("browser");
    self.onPageLoaded = function() { dump("onPageLoaded\n"); };
    self.onPrinted = function() { dump("onPrinted\n"); };
};

HtmlPrinter.prototype.loadPage = function(url) {
    var self = this;
    var listener = new WebProgressListener();
    listener.onNetworkDone = function() { self.onPageLoaded(); };
    self.browser.addProgressListener(listener, Components.interfaces.
				     nsIWebProgress.NOTIFY_STATUS);
    self.browser.loadURI(url, null, null);
};

HtmlPrinter.prototype.print = function(fileName, options) {
    var self = this;
    var ifreq = content.QueryInterface(Components.interfaces.
				       nsIInterfaceRequestor);
    var webBrowserPrint = ifreq.getInterface(Components.interfaces.
					     nsIWebBrowserPrint);
    var gPrintSettings = webBrowserPrint.globalPrintSettings;

    gPrintSettings.marginTop = -0.24;
    gPrintSettings.marginBottom = -0.24;
    gPrintSettings.marginLeft = -0.24;
    gPrintSettings.marginRight = -0.24;
    gPrintSettings.edgeLeft = 0;
    gPrintSettings.edgeTop = 0;
    gPrintSettings.edgeRight = 0;
    gPrintSettings.edgeBottom = 0;
    gPrintSettings.footerStrLeft = "";
    gPrintSettings.footerStrCenter = "";
    gPrintSettings.footerStrRight = "";
    gPrintSettings.headerStrLeft = "";
    gPrintSettings.headerStrCenter = "";
    gPrintSettings.headerStrRight = "";
    gPrintSettings.printToFile = true;
    gPrintSettings.printSilent = true;
    gPrintSettings.toFileName = fileName;
    gPrintSettings.paperWidth = options.width;
    gPrintSettings.paperHeight = options.height;
    //gPrintSettings.paperName = "Letter";
    gPrintSettings.showPrintProgress = false;
    // Adobe Postscript Drivers are expected (together with a FILE:
    // printer called "Generic PostScript Printer". Drivers can be
    // found here:
    // http://www.adobe.com/support/downloads/product.jsp?product=44&platform=Windows
    var runtime = Components.classes["@mozilla.org/xre/app-info;1"].
    getService(Components.interfaces.nsIXULRuntime);
    var OS = runtime.OS;
    if (OS == "WINNT")
	gPrintSettings.printerName = "Generic PostScript Printer";
    else
	gPrintSettings.printerName = "PostScript/default";

    var listener = new WebProgressListener();
    listener.onDone = function() { self.onPrinted(); };
	dump("Starting printing"+ new Date()+"\n");
    webBrowserPrint.print(gPrintSettings, listener);
	dump("Printing completed"+ new Date()+"\n");
};

function Html2Pdf(url, pdfFile, options) {
	dump("Initializing HTML printer object\n"+ new Date()+ "\n");
    var hp = new HtmlPrinter();
	dump("HTML printer initialized\n"+ new Date()+ " \n");
    hp.onPageLoaded = function() {
	var printit = function() {
	    try {
		dump("Starting"+ new Date()+ "\n");
		hp.print(pdfFile, options);
		dump("Printing"+ new Date() + "\n");
	    } catch (e) {
		dump(e + "\n");
		window.close();
	    }
	};
	window.setTimeout(printit, 0);
    };
    hp.onPrinted = function() {
	var quit = function() { window.close(); };
	window.setTimeout(quit, 0);
    };
    hp.loadPage(url);
}

function onLoad() {
    try {
	var wCmdLn = window.arguments[0];
	nsCmdLn = wCmdLn.QueryInterface(Components.interfaces.nsICommandLine);
	var options = { width : 6.4, height : 4.8 };
	var param;
	param = nsCmdLn.handleFlagWithParam("pw", false);
	if (param)
	    options.width = param;
	param = nsCmdLn.handleFlagWithParam("ph", false);
	if (param)
	    options.height = param;
	if (nsCmdLn.length != 2) {
	    dump("Wrong number of arguments. Expected <source> <destination>\n");
	    window.close();
	}
	var url = nsCmdLn.getArgument(0);
	var pdfFile = nsCmdLn.getArgument(1);
	dump(url);
 	var file = Components.classes["@mozilla.org/file/local;1"].getService(Components.interfaces.nsILocalFile);
	dump("done");
 	    file.initWithPath(urls);
	dump(file.path);
	var entries = file.directoryEntries;  
	 while(entries.hasMoreElements())  
		 {  
		  var entry = entries.getNext();  
 		  entry.QueryInterface(Components.interfaces.nsIFile);  
			dump(entry.path);
		  //if(entry.isDirectory() ) {


 }  

 

	Html2Pdf(url, pdfFile, options);
    }
    catch(e) {
	dump(e + "\n");
	window.close();
    }
}

function init() {
    addEventListener("load", onLoad, false);
}

init();


In this while loading the nsIFile component its throwing an error ReferenceError: urls is not defined.

If I remove the code and run this code only for single file works good but for directory iteration I require that.

Reproducible: Always

Steps to Reproduce:
1.run the code simply giving input and output path
2.
3.
This is not a security bug.  This is not even a bug in Mozilla, but a bug in your own code.  When you call |file.initWithPath(urls)| you are passing an undefined variable as the argument.  Everywhere else in your code you are using |url|.
Group: core-security
Status: UNCONFIRMED → RESOLVED
Closed: 14 years ago
Resolution: --- → INVALID
Yes I got it afterwords,

Now I have Modified the code.

Now I am giving I am iterating a directory and giving a single file one after the other for pdf generation but my code throws following exception

"[Exception... "Component returned failure code: 0x8048001f [nsIWebBrowserPrint.print]"  nsresult: "0x8048001f (<unknown>)"  location: "JS frame :: chrome://html2pdf/content/html2pdf.js :: anonymous :: line 124"  data: no]
460.800000 345.600000
36864 27648"

tHE CODE 

The code is as follows

	/*
	Convert HTML to PDF with mozilla rendering engine.
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; version 2 of the License.
	
	This program is distributed in the hope that it will be useful, but
	WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
	USA
	
	Michele Baldessari <michele@pupazzo.org> 2006
	Huang Ying <huang.ying.caritas@gmail.com> 2008
	*/
	
	function WebProgressListener() {
	var self = this;
	}
	
	WebProgressListener.prototype.
	QueryInterface = function(iid) {
	var self = this;
	if (iid.equals(Components.interfaces.nsIWebProgressListener) ||
		iid.equals(Components.interfaces.nsISupportsWeakReference) ||
		iid.equals(Components.interfaces.nsISupports))
		return self;
	
	throw Components.results.NS_ERROR_NO_INTERFACE;
	};
	
	WebProgressListener.prototype.
	onStateChange = function(webProgress, request, stateFlags, status) {
	var self = this;
	const WPL = Components.interfaces.nsIWebProgressListener;
	
	if (stateFlags & WPL.STATE_STOP) {
		if ("onDone" in self)
		self.onDone();
		if (stateFlags & WPL.STATE_IS_NETWORK)
		if ("onNetworkDone" in self)
			self.onNetworkDone();
	}
	};
	
	WebProgressListener.prototype.
	onProgressChange = function(webProgress, request, curSelf, maxSelf,
				curTotal, maxTotal) { };
	
	WebProgressListener.prototype.
	onLocationChange = function(webProgress, request, location) { };
	
	WebProgressListener.prototype.
	onStatusChange = function(webProgress, request, status, message) { };
	
	WebProgressListener.prototype.
	onSecurityChange = function(webProgress, request, state) { };
	
	function HtmlPrinter() {
	var self = this;
	self.browser = document.getElementById("browser");
	self.onPageLoaded = function() { dump("onPageLoaded\n"); };
	self.onPrinted = function() { dump("onPrinted\n"); };
	};
	
	HtmlPrinter.prototype.loadPage = function(url) {
	var self = this;
	var listener = new WebProgressListener();
	listener.onNetworkDone = function() { self.onPageLoaded(); };
	self.browser.addProgressListener(listener, Components.interfaces.
					nsIWebProgress.NOTIFY_STATUS);
	self.browser.loadURI(url, null, null);
	};
	
	HtmlPrinter.prototype.print = function(fileName, options) {
	var self = this;
	var ifreq = content.QueryInterface(Components.interfaces.
					nsIInterfaceRequestor);
	var webBrowserPrint = ifreq.getInterface(Components.interfaces.
						nsIWebBrowserPrint);
	var gPrintSettings = webBrowserPrint.globalPrintSettings;
	
	gPrintSettings.marginTop = -0.24;
	gPrintSettings.marginBottom = -0.24;
	gPrintSettings.marginLeft = -0.24;
	gPrintSettings.marginRight = -0.24;
	gPrintSettings.edgeLeft = 0;
	gPrintSettings.edgeTop = 0;
	gPrintSettings.edgeRight = 0;
	gPrintSettings.edgeBottom = 0;
	gPrintSettings.footerStrLeft = "";
	gPrintSettings.footerStrCenter = "";
	gPrintSettings.footerStrRight = "";
	gPrintSettings.headerStrLeft = "";
	gPrintSettings.headerStrCenter = "";
	gPrintSettings.headerStrRight = "";
	gPrintSettings.printToFile = true;
	gPrintSettings.printSilent = true;
	gPrintSettings.toFileName = fileName;
	gPrintSettings.paperWidth = options.width;
	gPrintSettings.paperHeight = options.height;
	//gPrintSettings.paperName = "Letter";
	gPrintSettings.showPrintProgress = false;
	// Adobe Postscript Drivers are expected (together with a FILE:
	// printer called "Generic PostScript Printer". Drivers can be
	// found here:
	// http://www.adobe.com/support/downloads/product.jsp?product=44&platform=Windows
	var runtime = Components.classes["@mozilla.org/xre/app-info;1"].
	getService(Components.interfaces.nsIXULRuntime);
	var OS = runtime.OS;
	if (OS == "WINNT")
		gPrintSettings.printerName = "Generic PostScript Printer";
	else
		gPrintSettings.printerName = "PostScript/default";
	
	var listener = new WebProgressListener();
	listener.onDone = function() { self.onPrinted(); };
		dump("Starting printing"+ new Date()+"\n");
	webBrowserPrint.print(gPrintSettings, listener);
		dump("Printing completed"+ new Date()+"\n");
	};
	
	function Html2Pdf(url, pdfFile, options) {
		dump("Html2PdfUrl input "+url+"\n");
		dump("Html2PdfUrl pdfFile "+pdfFile+"\n");
		dump("Initializing HTML printer object\n"+ new Date()+ "\n");
	var hp = new HtmlPrinter();
	dump("HTML printer initialized\n"+ new Date()+ " \n");
	var aFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
	aFile.initWithPath(url);
	var entries = aFile.directoryEntries;  
	while(entries.hasMoreElements())  
		{  
			var entry = entries.getNext();  
			entry.QueryInterface(Components.interfaces.nsIFile);  
			if(entry.isDirectory() ) {
				entries = entry.directoryEntries;  
			}else{
				if(entry.isFile){
					if(entry.path.indexOf(".html") != -1){
						var inputPath = entry.path;
						dump("input path "+ inputPath +"\n");
						var inputLeafName = entry.leafName;
						var name = inputLeafName.substr(0,inputLeafName.indexOf("."));
						dump("Modified NAme " + name+"\n");
						var outPutPath = pdfFile+"/"+name+".pdf";
						dump("outPutPath "+ outPutPath +"\n");
						hp.onPageLoaded = function() {
							var printit = function() {
							try {
								dump("Starting"+ new Date()+ "\n");
								hp.print(outPutPath, options);
								dump("Printing"+ new Date() + "\n");
							} catch (e) {
								dump(e + "\n");
								window.close();
							}
							};
							window.setTimeout(printit, 0);
						};
 						hp.onPrinted = function() {
 							var quit = function() { window.close(); };
 							window.setTimeout(quit, 0);
 						};
						hp.loadPage(inputPath);
					}
				} 
			}
		}
	}
	
	function onLoad() {
	try {
		var wCmdLn = window.arguments[0];
		nsCmdLn = wCmdLn.QueryInterface(Components.interfaces.nsICommandLine);
		var options = { width : 6.4, height : 4.8 };
		var param;
		param = nsCmdLn.handleFlagWithParam("pw", false);
		if (param)
		options.width = param;
		param = nsCmdLn.handleFlagWithParam("ph", false);
		if (param)
		options.height = param;
		if (nsCmdLn.length != 2) {
		dump("Wrong number of arguments. Expected <source> <destination>\n");
		window.close();
		}
		var url = nsCmdLn.getArgument(0);
		var pdfFile = nsCmdLn.getArgument(1);
		Html2Pdf(url, pdfFile, options);
	} catch(e) {
		dump(e + "\n");
		window.close();
	}
	}
	
	function init() {
	addEventListener("load", onLoad, false);
	}
	
	init();




and the line where the code is throwing an error is

webBrowserPrint.print(gPrintSettings, listener);
Please someone answer my question its very argent
This is a bug tracking database for Mozilla products, not a developer support tool.  Have you tried searching the error message?  Did you look at the line number that the error message refers to to debug your code?  You're going to need to demonstrate a little more resourcefulness if you want people to help you, in general.

https://developer.mozilla.org/
You need to log in before you can comment on or make changes to this bug.