The PhoneGap tutorial uses the Jspdf Library to generate PDF files in the application (PDF generation method) _android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.