ng2-pdf-viewer icon indicating copy to clipboard operation
ng2-pdf-viewer copied to clipboard

Rendering pdf from local on mobile

Open FrancescoCapelli-dev opened this issue 2 years ago • 1 comments

Bug Report or Feature Request (mark with an x)
- [ ] Regression (a behavior that used to work and stopped working in a new release)
- [ x ] Bug report -> please search issues before submitting
- [ ] Feature request
- [ ] Documentation issue or request

Hi Vadim I'm having trouble viewing local pdf on mobile. Ionic use and from the web it works perfectly. Do you have any ideas to help me?

FrancescoCapelli-dev avatar Jun 13 '22 09:06 FrancescoCapelli-dev

this works for me in ionic

import {Component, OnInit} from '@angular/core';
import {File} from '@ionic-native/file/ngx';

@Component({
    selector: 'example-app',
    template: `
  <pdf-viewer [src]="src"
              [render-text]="true"
              [original-size]="false"
              style="width: 100%; height: 100%"
  ></pdf-viewer>
  `
})
export class PdfViewerComponent implements OnInit {
    pdfSrc;
    src;

    constructor(private file: File) {}

    ngOnInit() {
        this.makeFileIntoBlob(this.pdfSrc);
    }

    makeFileIntoBlob(_imagePath) {
        // INSTALL PLUGIN - cordova plugin add cordova-plugin-file
        return new Promise((resolve, reject) => {
            let fileName = '';
            this.file
                .resolveLocalFilesystemUrl(_imagePath)
                .then((fileEntry) => {
                    let {name, nativeURL} = fileEntry;

                    // get the path..
                    let path = nativeURL.substring(0, nativeURL.lastIndexOf('/'));
                    console.log('path', path);
                    console.log('fileName', name);

                    fileName = name;

                    // we are provided the name, so now read the file into
                    // a buffer
                    return this.file.readAsArrayBuffer(path, name);
                })
                .then((buffer) => {
                    // get the buffer and make a blob to be saved
                    let imgBlob = new Blob([buffer], {
                        type: 'application/pdf'
                    });
                    console.log(imgBlob.type, imgBlob.size);
                    debugger
                    this.src = {data: buffer};
                    resolve({
                        fileName,
                        imgBlob
                    });
                })
                .catch((e) => reject(e));
        });
    }
}

a bit messy, still a work in progress

bricepowell avatar Jul 19 '22 15:07 bricepowell

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Oct 15 '22 16:10 stale[bot]