question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PDFViewer not working in mobile browsers?

See original GitHub issue

Describe the bug

It seems that displaying a PDF document inside the <PDFViewer> component is not working on mobile browsers (tested with Chrome and Firefox on Android).

To Reproduce

I created a simple demo create-react-app with the example PDF document from the react-pdf getting started guide: https://github.com/jhilden/react-pdf-demo

Expected behavior

That the PDF is rendered and displayed just like in desktop browsers.

Screenshots

Chrome 76.0.03809.132 on Android image

Clicking on the “Open” button will just open a new blank tab.

Firefox 68.1 on Android image

react-pdf version 1.6.4

Is this a known issue? If yes, it should probably be documented here https://react-pdf.org/components#pdfviewer And in that case it would be good to know how to test if the current environment supports PDFViewer. So one could e.g. use <PDFDownloadLink> instead, which is working fine in the those mobile browsers.

Or is this a bug that should normally be working?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:10
  • Comments:28 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
diegomuracommented, Aug 2, 2021

Unfortunately this is caused by mobile browsers not being able to render PDFs as a desktop browser usually does. In the near future I have plans to upgrade the PDFViewer component so it can render SVG docs, but it’s currently not possible. I can fallback to a download button if mobile browser detected, but this might not suit all the possible use cases as well.

I think it might be better to leave this to each one to decide on their app based on their needs. usePDF can be used and based on desktop or browser, each can render either an iframe or a download link for mobile (or whatever suits your app best). Thoughts?

4reactions
ronaldaraujocommented, Nov 2, 2021

Sure, @TheoOliveira. Below is the code I implemented.

Device component

import { ReactNode } from 'react';
import * as rdd from 'react-device-detect';

type DeviceProps = {
  children: (props: typeof rdd) => ReactNode;
};

export default function Device(props: DeviceProps) {
  return <div className="device-layout-component">{props.children(rdd)}</div>;
}

Report

<Device>
      {({ isMobile }) => {
        if (isMobile) {
          return (
            <PDFDownloadLink
              document={<MyDocument deas={deas} />}
              fileName="YOUR_FILE_NAME"
            >
              {({ loading }) =>
                loading ? "Loading...": <ModalPDFGenerated />
              }
            </PDFDownloadLink>
          );
        }
        return (
          <PDFViewer style={{ width: '100%', height: '100vh' }}>
            <MyDocument .../>
          </PDFViewer>
        );
      }}
    </Device>

ModalPDFGenerated and MyDocument are external components that are part of the implementation. But I believe the central idea is this. Hope this helps. Hug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PDF Viewer does not work on mobile?
Apparently, there is no PDF viewer in Chrome for Androïd. https://support.google.com/chrome/thread/68561173/android-chrome-browser-don-t-preview-pdf?
Read more >
PDF Viewer support in mobile browser (iPhone & Android)
No browsers can support native rendering of PDF files without a plugin (except Google Chrome as claimed here).
Read more >
Chrome PDF Viewer Not Working? Here's How to Fix It
Use these solutions to fix Chrome PDF's Viewer so you can keep using your favorite browser.
Read more >
PDF Not Opening In Chrome Browser: Solved [8 Possible Fixes]
There can be multiple reasons behind Chrome PDF's viewer not working error. Few reasons being like excess cookies, because of an extension, ...
Read more >
android chrome browser don't preview pdf - Google Support
The Android Chrome Browser APP doesn't support plug-ins, so it doesn't have a Chrome PDF Viewer. There for a PDF file can't natively...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found