pipwerks / PDFObject

A lightweight JavaScript utility for dynamically embedding PDFs in HTML documents.
http://pdfobject.com/
Other
2.39k stars 986 forks source link

Automatic download for mobile browsers in full-browser embed #302

Closed a-w-1806 closed 3 months ago

a-w-1806 commented 3 months ago

First, thank you so much for this really awesome tool!

I understand that when you open the page in a mobile browser you will be presented a fallback message. I am just wondering if there is any way that it can detect that it's opened in a browser that does not support built-in PDF viewer, and automatically downloads and opens in the browser? I am using it as a full-browser embed, and I feel that this can make the mobile UX more friendly and intuitive.

Thanks!

pipwerks commented 3 months ago

You can achieve that goal with the current system, just use the returned value of pdfobject.embed to determine whether the browser supports inline PDFs. If no, instruct the browser to open the PDF in a new window/tab.

let success = PDFObject.embed("myfile.pdf", "#my-container");
if(!success){
   window.open("myfile.pdf", "nameThisTabWhateverIsAppropriate");
}

or replace current browser page with PDF

let success = PDFObject.embed("myfile.pdf", "#my-container");
if(!success){
   window.location = "myfile.pdf";
}

Note: I haven't tested this snippet, you may need to tweak it. For example, you may need to provide a fully qualified URL instead of a relative URL (e.g. "https://example.com/myfile.pdf"). But this should point you in the right direction.