Skip to content Skip to sidebar Skip to footer

Save Pdf Directly To File Using Jspdf

My script is a Firefox addon so has access to sensitive code like access to filesystem etc. I display a Panel populated with html content, I could easily send input to the addon co

Solution 1:

This should work on your Firefox add-on code:

const { OS } = require("resource://gre/modules/osfile.jsm");

var pathToFile = OS.Path.join("path", "to", "file.pdf");

var doc = newjsPDF();
doc.text(20, 20, 'Hello bob');

var ab = doc.output('arraybuffer');
var u8 = newUint8Array(ab);

OS.File.writeAtomic(pathToFile, u8).then(
   function()
   {
       alert('File written!');
   },
   function(e)
   {
       alert('Error ' + e);
   }
);

If you aren't using the Add-On SDK, but rather a normal extension, replace the first line with:

const { OS } = Components.utils.import("resource://gre/modules/osfile.jsm", {});

Check this out for further info on OS.File: https://developer.mozilla.org/en-US/docs/JavaScript_OS.File/OS.File_for_the_main_thread

Solution 2:

But why you don't wanna use your add-on? I think that you don't be able to access the filesystem or things like that, with jsPDF or pdf.js (http://mozilla.github.io/pdf.js/, which is a mozilla project), you will be able to print something that is being displayed on the window, but not sure if you can access some local files.

Post a Comment for "Save Pdf Directly To File Using Jspdf"