简体   繁体   English

如何在模态中显示 pdf 而不是在 React js 中以新的 window 打开它

[英]how to show the pdf in a modal instead of opening it in a new window in react js

I have created a pdf file using blob text in react js and using "window.open(fileURL, "_blank")" i am able to see the pdf in a new window.我在 React js 中使用 blob 文本创建了一个 pdf 文件,并使用“window.open(fileURL, “_blank”)”,我能够在新的 window 中看到 pdf。

But now my requirement is just to show the pdf as a modal in ui and when clicked on the modal it can be viewed in another window.Can anybody please help on this.但现在我的要求只是在 ui 中将 pdf 显示为模态,当单击模态时,可以在另一个 window 中查看它。有人可以帮助解决这个问题吗?

Below is my code snippet:下面是我的代码片段:

var oReq = new XMLHttpRequest();

 var URLToPDF = baseUrl+"/downloadPDF

  oReq.open("GET", URLToPDF, true);

oReq.responseType = "blob";
var that = this;
oReq.onload = function() {

    const pdfFile = new Blob([oReq.response], { type: 'application/pdf' });

    const fileURL = URL.createObjectURL(pdfFile);

     window.open(fileURL, "_blank");

};
   oReq.send();

You can add iframe with inside your modal.您可以在模态内添加 iframe。

 <div className="modal">
     <div className="modalContent">
         <iframe src="http://www.africau.edu/images/default/sample.pdf" style="width:600px; height:500px;" frameborder="0"></iframe>
     </div>
 </div>
I have used @mantine components 
  import {modal,Anchor} from '@mantine/core'
   
 const showFilesInModal=()=>{
    const [isModalOpened, setIsModalOpened] = useState<boolean>(false);

return (<>
 <Anchor
                            className="underline"
                            onClick={() => setIsModalOpened(true)}
                          >
                            <Modal
                              opened={isModalOpened}
                              onClose={() => setIsModalOpened(false)}
                              title="Files"
                              size="lg"
                            >
                              {/* Modal content */}
                              <div className="h-max w-max">
                                <img src="your file source"
                                />

                              </div>
                            </Modal>
                               link
                          </Anchor>
                       
</>)
}

To open your pdf in new window rather than new tab you can use this lines of native JS code要在新的 window 而不是新选项卡中打开 pdf,您可以使用这行本机 JS 代码

const fileURL = URL.createObjectURL(blob);
let newWin = window.open(fileURL, "pdf show", "width=800,height=600");
newWin.focus();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM