First create a PhoneGap project on the command line
Copy Code code as follows:
PHONEGAP Create. "Jspdf.sample" "Jspdf App"
PhoneGap Local plugin Add Org.apache.cordova.file
PhoneGap Local plugin Add https://git-wip-us.apache.org/repos/asf/cordova-plugin-console.git
Then, download the jspdf code download the Jspdf project code and copy the target code to the PhoneGap engineering directory. I put it under the Www/js. The file is then introduced in the main HTML file.
Copy Code code as follows:
<script type= "Text/javascript" src= "Js/jspdf.source.js" ></script>
I use a source file that is not compressed/minimized in the ' dist ' directory.
Next we start to build the PDF file. The following code fragment utilizes the PhoneGap file processing API PhoneGap ' s file API. To generate a simple PDF file and save it to the local device. This should be considered the *after* the Deviceready event.
Where Console.log is used only for debugging purposes:
Copy Code code as follows:
GENERATE the PDF DOCUMENT
Console.log ("Generating pdf ...");
var doc = new jspdf ();
Doc.text (' hello! ');
Doc.setfont ("Courier");
Doc.setfonttype ("normal");
Doc.text, ' This is a PDF document generated using Jspdf. ');
Doc.text (M, ' YES, Inside of phonegap! ');
var pdfoutput = Doc.output ();
Console.log (Pdfoutput);
NEXT SAVE IT to the DEVICE ' S local FILE SYSTEM
Console.log ("File system ...");
Window.requestfilesystem (localfilesystem.persistent, 0, function (filesystem) {
Console.log (Filesystem.name);
Console.log (FileSystem.root.name);
Console.log (FileSystem.root.fullPath);
FileSystem.root.getFile ("Test.pdf", {create:true}, function (entry) {
var fileentry = entry;
Console.log (entry);
Entry.createwriter (function (writer) {
Writer.onwrite = function (evt) {
Console.log ("write success");
};
Console.log ("Writing to File");
Writer.write (Pdfoutput);
}, function (Error) {
Console.log (Error);
});
}, function (Error) {
Console.log (Error);
});
},
function (event) {
Console.log (Evt.target.error.code);
});
The PDF creation process is actually very simple. You can do so simply by using doc.output () to get the string ID of the created file. Whether it's saved locally, sent to the server, or even sent directly to a PDF reader on the local device.