Save pdf in App. From a JSON with base64. Cordova Phonegap File or File transfer. writer.truncate (11) writer.seek (4)

0

Hi, I have a question about how to convert a base64 to pdf with jquery or javascript.

And also what are the numbers that are passed by parameter to the functions = > writer.truncate(11) writer.seek(4) and in case you want to save a pdf as you should.

This is my current code

json = { DocArchivoB64:"JVBERi0xLj.......=" //base64
DocArchivoNombre:"CARTA.pdf"
DocCodigo:1
DocEmpresa:null
DocFecha:"2018-06-22T13:00:00"
DocObservacion:""
}
  • How to convert base64 to pdf?
    It is right? The phone gives error message when opening.

  • In the gotFileWriter(writer) function that are for the numbers that are passed as parameters writer.truncate(11) writer.seek(4)

    function base64Tobinaty(jsonMasImg) {
        var base64str = jsonMasImg.DocArchivoB64;
    
        var binary = atob(base64str.replace(/\s/g, ''));
        var len = binary.length;
        var buffer = new ArrayBuffer(len);
        var view = new Uint8Array(buffer);
        for (var i = 0; i < len; i++) {
            view[i] = binary.charCodeAt(i);
        }
        // create the blob object with content-type "application/pdf"               
        var blob = new Blob([view], {type: "application/pdf"});
        var url = URL.createObjectURL(blob);
        return blob;
    }
    
    
    var documentoDescargado = null;
    
    function descargarDocumento(jsonMasImg) {
        documentoDescargado = jsonMasImg; // obtengo un objeto binario y
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
    }
    
    function gotFS(fileSystem) {
        var nombreDoc = documentoDescargado.DocArchivoNombre;
        alert(nombreDoc);
        fileSystem.root.getFile( nombreDoc, {create: true, exclusive: false}, gotFileEntry, fail);
    
    }
    
    function gotFileEntry(fileEntry) {
        fileEntry.createWriter(gotFileWriter, fail);
    }
    
    function gotFileWriter(writer) {
        var pdf =  base64Tobinaty(documentoDescargado);
    
        writer.onwriteend = function (evt) { writer.truncate(); writer.onwriteend = function (evt) { writer.seek(); writer.write( ); writer.onwriteend = function (evt) {}; }; };
        alert("paso el onwriteend");
        writer.write(pdf);
    }
    
    function fail(error) {
        alert(error.code);
    }
    
  • Thank you all for sharing your opinions, improvements or suggestions.

    SOLVED! Correct code. Hello Readers, despite not knowing what or how it works writer.truncate (11) writer.seek (4) I want to inform you that the code you put up is perfect. Of all medos if someone can explain these two methods would be a great contribution. Since removing the 11 and 4 remain working. Clarification to this is missing a notice that has been saved or open the location of the file.

        
    asked by Juan Saa 20.07.2018 в 17:05
    source

    0 answers