IONIC 3 and jsPDF , using FILE Transfer to download pdf not working

一个人想着一个人 提交于 2019-12-23 04:56:23

问题


i want to use jsPDF library with IONIC3 to download PDF , i have created a simple document and when i try to download it with the default function from jsPDF doc.Save() it work in my browser , but in a real device won't work. So i decided to use FileTransfer Plugins from IONIC native , i have firstly created a blob File from jsPDF output , then i tried to use the writeFile() function from IONIC FILE plugin to create a new file from the blob , then i tried to download that file using Download() function from FileTransfer , Here is my code : `

createPdf(){
    const fileTransfer: FileTransferObject = this.transfer.create();
    //Initialize jsPdf
    let doc = new jsPDF();
    doc.setFontStyle('Bold');
    doc.setFontSize(14);
    doc.text('Testing PDFs', 20, 20);
    // doc.save("example.pdf") This is the default way to download pdf In jsPDF , but it's not working in ionic
    // So Here i create new File from the blob
    let blob = doc.output('blob', {type: 'application/pdf'});
    this.file.writeFile(this.file.dataDirectory,"example.pdf",blob)
    .then((value)=>{
              console.log("File created successfly" + value);
              // if The file successfuly created , i want to download it
              fileTransfer.download( this.file.dataDirectory+"example.pdf" ,this.file.dataDirectory+"example.pdf")
                .then((entry)=>{
                  console.log("Download Success : " , entry);
                })
                .catch((error)=>{
                      console.log("Error when downloading file : " ,error);
                })
    })
    .catch((error)=>{
      console.log("Error creating File");
    });
)

`

if i look to my output console everything work ok , but nothing Happen !!

[19:50:26] console.log: File created successfly{"isFile":true,"isDirectory":false,"name":"example.pdf","fullPath":"/example.pdf","filesystem":"","nativeURL":"file:///data/user/0/io.ionic.starter/files/exam

[19:50:26] console.log: Download Success : {"isFile":true,"isDirectory":false,"name":"example.pdf","fullPath":"/example.pdf","filesystem":"","nativeURL":"file:///data/user/0/io.ionic.starter/files/example.pdf"}


回答1:


Please use below code :

  let pdfOutput = doc.output();
  // using ArrayBuffer will allow you to put image inside PDF
  let buffer = new ArrayBuffer(pdfOutput.length);
  let array = new Uint8Array(buffer);
  for (var i = 0; i < pdfOutput.length; i++) {
      array[i] = pdfOutput.charCodeAt(i);
  }

  const directory = this.file.externalApplicationStorageDirectory ;

  const fileName = "example.pdf";

  //Writing File to Device
  this.file.writeFile(directory,fileName,buffer)
  .then((success)=> console.log("File created Succesfully" + JSON.stringify(success)))
  .catch((error)=> console.log("Cannot Create File " +JSON.stringify(error)));


来源:https://stackoverflow.com/questions/46918252/ionic-3-and-jspdf-using-file-transfer-to-download-pdf-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!