7

I'm trying to load pdf file inside of iFrame in REACT, the code looks like this:

render(){
   return(
      <iframe
          sandbox="allow-same-origin allow-scripts allow-forms"
          title="PortletIFrameWidget"
          src={'.resources/crayola.pdf'}
          ref={(f) => { this.ifr = f }}
      />
   )
}

and I'm getting a warning:

Resource interpreted as Document but transferred with MIME type application/pdf: "http://localhost:8080/resources/crayola.pdf"

but when I go to the link: "http://localhost:8080/resources/crayola.pdf" I can easily see the content of PDF, so there is the pdf file, the problem is: it's not being uploaded/rendered in iFrame. Why?

THIS WORKS IN MOZILLA FIREFOX, AND IN EDGE, but not in CHROME.

any idea why is this happening? And how to load PDF file in iFrame in REACT

2

3 Answers 3

5

this has nothing to do with React, the problem obviously is in GOogle-Chrome. here's the solution I made:

<iframe src="./resources/crayola.pdf" title="title">
     Presss me: <a href="./resources/crayola.pdf">Download PDF</a>
</iframe>

and the link that helped me: PDFobject

Sign up to request clarification or add additional context in comments.

Comments

4

Try using an embed tag:

<embed src={'.resources/crayola.pdf'} type="application/pdf"/>

Comments

1

In React, Use Ref in Functional Component and set after your initial rendering using setAttribute

function App() {
  const refIframe: any = useRef(null);
  
  useEffect(() => {
       refIframe.current.setAttribute(
        'src',
        './resources/crayola.pdf',
    );
  }, []);
  return (
      <iframe
        title="Iframe Doc"
        ref={refIframe}
      />
  );
}

export default App;

1 Comment

Why do this over just setting the attribute statically?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.