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

Viewing PDFs with PDF.js #58

Open
imhoffd opened this issue Oct 26, 2020 · 12 comments
Open

Viewing PDFs with PDF.js #58

imhoffd opened this issue Oct 26, 2020 · 12 comments

Comments

@imhoffd
Copy link

imhoffd commented Oct 26, 2020

https://github.com/mozilla/pdf.js

ref: capacitor-community/proposals#15 (comment)

@lmsantanam
Copy link

Hi, thanks for creating this, so is there no solution on how to simply open a pdf with an app using capacitor? I've been trying since last week with this and now I'm considering switching back this app to Cordova

@imhoffd
Copy link
Author

imhoffd commented Nov 10, 2020

How are you doing this in Cordova?

@lmsantanam
Copy link

How are you doing this in Cordova?

I'm not, this is my first time working with pdf files in Ionic. But for Cordova I've found a bit more information. Right now I'm following this tutorial to see if maybe I'm lucky

https://www.youtube.com/watch?v=k2iaGNApM9w

@muuvmuuv
Copy link

Anyone made some progress here? I am also tinkering about it. But I guess opening it in the build in browser and let that one display his own PDF controls is the easisiest solution right now. I would really like to not install 4 cordova plugins do display PDF's.

@lmsantanam
Copy link

Hi, in my case, I ended up using this plugin:

https://github.com/stephanrauh/ngx-extended-pdf-viewer

Is originally designed for Angular but adapting to Ionic was not very difficult

@muuvmuuv
Copy link

muuvmuuv commented Aug 5, 2021

@LuisManuel1983 how did you got it working with Capacitor urls? I mean capacitor://localhost/_capacitor_file..... Here it just says "Unexpected server response (0) while retrieving pdf ---".

@lmsantanam
Copy link

@muuvmuuv sorry I've only worked with standard URL's to show a PDF that is hosted online, not local files. That "capacitor link" is unknown to me... btw did you try the plugin I suggested?

https://github.com/stephanrauh/ngx-extended-pdf-viewer

If your error is with that plugin I would suggest to look at the issues there, the creator of that plugin is really awesome and helpful

@muuvmuuv
Copy link

muuvmuuv commented Aug 9, 2021

@LuisManuel1983 I am already using that plugin. I guess it just has something to do with pdj.js trying to parse the url different when having a custom protocol.

@elvisgraho
Copy link

elvisgraho commented Oct 28, 2021

Works with Ionic & Capacitor (pdf does not load with live reload when developing, because of CORS, but works fine otherwise)

https://github.com/stephanrauh/ngx-extended-pdf-viewer can use your local capacitor// url to open locally stored files.

      const uriResult = await Filesystem.getUri({
        directory: Directory.Data,
        path: `filename.pdf`,
      });

      // the path to the file
      const path = uriResult.uri;

      // convert to capacitor//
      const localFileURL = Capacitor.convertFileSrc(path);

(you can also get the url with cordova and old ionic plugins)

@jcesarmobile jcesarmobile transferred this issue from ionic-team/capacitor-site Aug 25, 2022
@buckmower
Copy link

Is there a solution for a React based Ionic App? I'm getting the error:

[Log] PDFJS.express: Development environment detected. This license key is currently registered to {mydomain} (PDFJSDocumentType.js, line 64020, x2)

The problem seems to be that capacitor://localhost isn't {mydomain} and furthermore doesn't qualify as localhost according to PDFJS.

@cyz1901
Copy link

cyz1901 commented Feb 11, 2024

Perhaps we can write a plugin that uses Swift and Kotlin native APIs to display PDF?

@liza-nt
Copy link

liza-nt commented Jul 1, 2024

I have url of pdf which i am downloading using capacitor-community/http for file opener I am using capacitor-community/file-opener
I am getting error like this
File Dest file:///Users/parameswarmeher/Library/Developer/CoreSimulator/Devices/DC5D6B85-FD86-4D98-BE33-D7A767C0415C/data/Containers/Data/Application/FB862B43-FC24-458D-A5FD-FC9FA7C181A5/Documents/DOCUMENTS/test.pdf
ERROR MESSAGE: {"message":"Unable to download file","errorMessage":"Unable to download file","code":"DOWNLOAD"}
⚡️ [error] - {"message":"Unable to download file","errorMessage":"Unable to download file","code":"DOWNLOAD"}
⚡️ [error] - Error downloading or opening the file: {"code":"DOWNLOAD","errorMessage":"Unable to download file"}

could you help me what I am doing wrong

here is my code

    async getPdfBase64(url: string): Promise<void> {
        try {
            const remoteUrl = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
            const fileName = 'test.pdf'

            // Define the file path and options
            const filePath = `${Directory.Documents}/${fileName}`;

            // Download the file
            const options: any = {
                url: remoteUrl,
                filePath: filePath,
                fileDirectory: Directory.Documents,
                method: 'GET',
            };
            const response: any = await Http.downloadFile(options);
            console.log("respo", response)

            // Check if the file was downloaded successfully
            if (response.status !== 200) {
                throw new Error(`Failed to download file: ${response.statusText}`);
            }

            const fileOpenerOptions: FileOpenerOptions = {
                filePath: filePath,
                contentType: 'application/pdf',
                openWithDefault: true,
            };
            await FileOpener.open(fileOpenerOptions);
            console.log('File downloaded and opened successfully!');
        } catch (error) {
            console.error('Error downloading or opening the file:', error);
        }
    }

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

7 participants