parallax / jsPDF

Client-side JavaScript PDF generation for everyone.
https://parall.ax/products/jspdf
MIT License
28.99k stars 4.64k forks source link

image output base64 on cordova #3048

Closed brunoalex closed 3 years ago

brunoalex commented 3 years ago

im trying to convert images from galery into a pdf but my pdf dont display my images

my code:

   ```

function takepic(){

navigator.camera.getPicture(onSuccess, onFail, { quality: 25, destinationType: Camera.DestinationType.DATA_URL });

function onSuccess(imageData) { var image = document.getElementById('myImage'); image.src = "data:image/jpeg;base64," + imageData; }

function onFail(message) { alert('Failed because: ' + message); }

}

function getpic(){ navigator.camera.getPicture(onSuccess, onFail, { quality: 25, destinationType: Camera.DestinationType.DATA_URL, sourceType: Camera.PictureSourceType.PHOTOLIBRARY });

function onSuccess(imageURL) {

  var image = document.getElementById('myImage');
 image.src = "data:image/jpeg;base64," + imageURL;

basei=imageURL; var doc = new jsPDF(); var imgData = 'data:image/jpeg;base64,'+ imageURL;

// var pdf = new jsPDF('p','pt','a4'); // pdf.text(20, 20, 'YES, Inside of Cordova!'); // pdf.addImage(imageURL, 'png', 30, 80); // var pdfOutput = pdf.output("blob");

// //FIRST GENERATE THE PDF DOCUMENT console.log("generating pdf..."); var doc = new jsPDF();

// doc.text(20, 20, 'HELLO!');

// doc.setFont("courier"); // doc.setFontType("normal"); // doc.text(20, 30, 'This is a PDF document generated using JSPDF.'); // doc.text(20, 50, 'YES, Inside of cordova!');

doc.addImage(imgData, 'JPEG', 15, 40, 175, 75);

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;

     filep= entry.nativeURL;
   console.log(filep);

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 ); });

setTimeout(function() { cordova.plugins.fileOpener2.open( filep, // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Downloads/starwars.pdf 'application/pdf', { error : function(e) { console.log('Error status: ' + e.status + ' - Error message: ' + e.message); }, success : function () { console.log('file opened successfully'); }}, 4000); }

);

//

}

function onFail(message) { alert('Failed because: ' + message); } }


can any one give me a hand ? tks
HackbrettXXX commented 3 years ago

Please clean up your code sample (reduce it to what's really necessary to reproduce the issue) and surround it with ```. My first guess would be you might run into CORS restrictions. If that's not the case, please clarify the issue. Please also check if there are any errors on the developer console, exceptions, etc.

github-actions[bot] commented 3 years ago

This issue is stale because it has been open 90 days with no activity. It will be closed soon. Please comment/reopen if this issue is still relevant.