Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

javascript - why cordova dont save jspdf?

the code works only by starting it from the browser, while in the phone the button does not generate pdf ... I state that I've been using Cordova recently and maybe I can't set the plugins well .. any information, even trivial, can be useful. the cart table is generated by a javascript code that uses the dom on another hatml page, I think that something is almost certainly missing to make the app store the file on the device but I don't know what to insert .. also which plug in to refer to possibly .. Thanks in advance to everyone

<strike>
 <HTML>
    <HEAD>
       <LINK REL="stylesheet" TYPE="text/css" href="style.css"> 
               
     
       <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
      <script src="js/carrello.js"></script>
       
    </HEAD>
    
    <BODY>
        <div>
          <h2>Il tuo Ordine</h2>
                 
         <script language=javascript >
  
            carrello = eval(localStorage.carrello);
            document.write("<div id=elenco>");
            tabella(); 
            document.write("</div>");
            totali();
            
         </script>
       </div>
    <br>
    <br>
       <a class="pTab" href=prodotti.html style="text-decoration: none;">Torna ad ordinare</a>
       <br>
       <br> 
       <a class="pTab" href=javascript:svuota() style="text-decoration: none;">Svuota ordine</a>
     <br>
     <br>
     <br>
        <button onclick="scaricaPdf()">crea PDF</button>
    <br>
    <br>
   
      <a href="index.html" style="text-decoration: none;">
        <div class="mybutton1">HOME</div>
    </a>
    </BODY>
      <script> 
       
function scaricaPdf() {
       var doc = new jsPDF();

       var spesaTotale= document.getElementById("totale").innerHTML;
       var x=20; //variabile per i margini
       var y=35; //variabile per le righe
       doc.setFontSize(15);
       doc.text(20,20,'Questo è il tuo ordine:');
       doc.setFontSize(10);
       doc.text(20,30,'Codice'+'  Descrizione Prodotto' +'  Prezzo'+'  Quantità'+'   Totale'); 
       for(var i=0; i<carrello.length; i++){
            y=y+5;
            var codiceProdotto=carrello[i].codice;
            doc.text(x,y,' '+codiceProdotto);
            var descrizioneProdotto = carrello[i].descr;
            doc.text(33,y,''+descrizioneProdotto);
            var prezzoProdotto = carrello[i].prezzo;
            doc.text(68,y,''+prezzoProdotto);
            var quantitaProdotto =carrello[i].qnt;
            doc.text(83,y,''+quantitaProdotto);
            var totProdEuro=carrello[i].prezzo * carrello[i].qnt;
            doc.text(100,y,''+totProdEuro);
         }
       
       
       doc.text(20,200, 'Totale        '+spesaTotale);
       doc.save('Ordine.pdf');
}

       </script>
      
      <script src="cordova.js"></script>
      <script src="js/index.js"></script>
    </HTML>
    

</strike>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can not use JSPDF to save files on a mobile phones, at least to my knowledge. In my experience I would try looking at Cordova documentation online. For example: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/#write-to-a-file-

If you want to use File plugin make sure you install it first and go through documentation! https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html#installation

There also seems to be Cordova generator plugin, but i don't have any experience with it https://www.npmjs.com/package/cordova-pdf-generator

Either way, I would advise you to refactor your code without jspdf.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...