Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add local images after upgrading to Android API 30 #141

Open
iactiva opened this issue May 27, 2022 · 1 comment
Open

Unable to add local images after upgrading to Android API 30 #141

iactiva opened this issue May 27, 2022 · 1 comment

Comments

@iactiva
Copy link

iactiva commented May 27, 2022

Hi there,

There seems API 30 is blocking anyhow embedding local images with pdf-creator. I'm desperate since I've tried everything I could figue as solution.

The PDF file is properly created and it is correctly readable however images (any local content) are not displaying, although remote https images still work. It stopped working after upgrading from Android API 29 to API 30, and API 30 is now mandatory. Are you experiencing the same issue?

Everything I've tried displays the images in html using img [src]=“IMAGE”, but images are no longer displayed in the PDF file.

What I've tried:

  1. Using base64 images:
    IMAGE = this.sanitizer.bypassSecurityTrustUrl("data:image/jpeg;base64," + b64);

  2. Using local image url:

let url = this.file.dataDirectory + img;
const win: any = window;
const fixedURL = win.Ionic.WebView.convertFileSrc(url);
IMAGE = this.sanitizer.bypassSecurityTrustResourceUrl(fixedURL);

or
IMAGE = IMAGE['changingThisBreaksApplicationSecurity’]

  1. Using asset relative URL.

  2. Using local path:
    IMAGE = this.file.dataDirectory + img

I'd appreciate any help.

My code:

let htmlSample="<img style='width:150px' src=‘"+IMAGE+"' />";
      let options = {
      documentSize: 'A4',
      type: 'base64'
    }
 
  this.pdfGenerator.fromData(htmlSample, options).
      then((base64)  => {
        var fileName="generated";
        this.saveBase64(base64, fileName)
  .then(obj => {
   if (obj) {
          this.showPDF(obj);
   }
  }).catch(e => {
  });
});
 
showPDF(file) {
  let filePath = this.file.dataDirectory;
  let namefile=file;
  this.fileOpener.open(filePath+namefile, 'application/pdf');
}
 
 saveBase64(base64:string, name:string):Promise<string>{
    return new Promise((resolve, reject)=>{
      let UUID = name+ (new Date().getTime()).toString(16);
      let blob=this.b64toBlob(base64, 'application/pdf')
      this.file.writeFile(this.file.dataDirectory, UUID + '.pdf', blob, {replace: true})
                .then(fe => {
                  return resolve(
                    UUID + '.pdf'
                    );
                }).catch(err => {
                  reject(err);
                })
    })
  }
 
b64toBlob(b64Data, contentType) {
    contentType = contentType || '';
    var sliceSize = 512;
    var byteCharacters = atob(b64Data);
    var byteArrays = [];
    for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) {
      var slice = byteCharacters.slice(offset, offset + sliceSize);
      var byteNumbers = new Array(slice.length);
      for (var i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }
      var byteArray = new Uint8Array(byteNumbers);
      byteArrays.push(byteArray);
    }
    var blob = new Blob(byteArrays, {type: contentType});
    return blob;
  }

Conditions:
"@ionic-native/pdf-generator": "^5.36.0”,
Latest cordova-pdf-generator plugin
"cordova-android": "^10.1.2”,
Angular 8
Ionic 5
Android API 30

@iactiva iactiva changed the title Unable to add local images after upgradint to Android API 30 Unable to add local images after upgrading to Android API 30 May 27, 2022
@pchitrakar-tt
Copy link

I am having the same issue. I cannot get the image into pdf. I like this plugin which is very simple and easy to use and don't really want to switch to other library but I may have to use jspdf or pdfmake for better support. Please fix this bug soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants