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.

PDF Doesn't display, Loading PDF message displayed. No Errors.

See original GitHub issue

Before you start - checklist

  • I followed instructions in documentation written for my React-PDF version
  • I have checked if this bug is not already reported
  • I have checked if an issue is not listed in Known issues
  • If I have a problem with PDF rendering, I checked if my PDF renders properly in PDF.js demo

Description

I have to download a PDF from a NodeJS API and render it. The results of the API call render in both the react-pdf test page and also in Firefox. I’ve also hard-coded the path to a PDF file and that renders fine.

But when used in my app, I get the “Loading PDF” message and that never goes away and tthe PDF doesn’t render.

My code is simple enough:

import React, { Component } from 'react';
import { Document, Page } from 'react-pdf/dist/esm/entry.webpack';
import pdfFile from './CMS-2021-0070-0003-attachment_1.pdf';

class CommentPartPDF extends Component {
	constructor(props) {
		super(props);
		this.state = {
			pdf: null,
			pages: 0,
		};  
	}
	
	componentDidMount() {
		fetch(`/api/${this.props.office}/getPdf/${this.props.commentId}/${this.props.attNum}`)
		.then((data) => {
			this.setState({pdf: data});
		});
	}
	
	setPages = (p) => {
		this.setState({pages: p});
	}

	render() {  
		if (this.props.attNum >= 1 && this.state.pdf != null) {
			var pdf=this.state.pdf;
			return (
				<Document file={{data: pdf.data}}
					onLoadError={(e) => console.log(`PDF LOAD ERROR: ${JSON.stringify(e)}`)}
					onLoadSuccess={(p) => this.setPages(p)}>
					<Page pageNumber={1} />
				</Document>
			)
		} else {
			return null;
		}
	};
}
export default CommentPartPDF;

I suspect I’m not setting up the Document file attribute correctly, Any help would be great.

Steve

Steps to reproduce

Simply run the component as above

Expected behavior

Render and display the PDF

Actual behavior

Loading PDF is displayed forever and PDF file isn’t displayed

Additional information

None

Environment

  • **Browser (if applicable): Chrome 96, Firefox 94
  • React-PDF version: 5.7.1
  • React version: 16.14.0
  • Webpack version (if applicable): none

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
Tenpicommented, Sep 1, 2022

I had the same issue, and I managed to pinpoint the error.

"Error: The API version "2.12.313" does not match the Worker version "2.16.105"."

In my case I was using the most recent version of pdfjs-dist (to set the worker) but the most recent version of this package only supports up to 2.12.313.

This was the fix (on 5.7.2 of this package):

npm i pdfjs-dist@2.12.313

Also, loading local PDFs did not work for me. You have to convert them to URL as follows:

const blob = await axios.get(pdf, {responseType: "blob"}).then((r) => r.data)
const url = URL.createObjectURL(blob)
1reaction
scartoncommented, Mar 19, 2022

Well, it looks like a stream of data 😃 When I plug the URL into the test site, it renders fine. and I can tweak the headers on the response (I wrote the API too) and get a download which opens in FF and Chrome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot viewing PDF files on the web - Adobe Support
In Reader or Acrobat, right-click the document window, and choose Page Display Preferences. · From the list at left, select Internet. · Deselect ......
Read more >
Effective Methods to Fix "Cannot Open PDF" Error
Part 1: Why does "PDF Not Opening" Error Occur? · Unsupported file type: This is the most common reason you cannot open PDF....
Read more >
PDF Doesn't Appear At All - Just A Link Or Hangs On 'Loading...'
If the PDF doesn't display at all then please get in touch with our support team via email. Send a link to the...
Read more >
Why am I getting the error message “Failed to Load PDF ...
The “Failed to Load PDF Document” error message indicates that the web browser you are using, Google Chrome, is trying to open the ......
Read more >
error in displaying pdf in react-pdf - Stack Overflow
It's likely the document path: file="./1.pdf" . This looks like a server-side path. Try to use absolute or relative URL to your file...
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