最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - New tab opened with server-generated byte array PDF in ReactNextJS suppressed with popup blocker - Stack Overflow

programmeradmin2浏览0评论

I read the similar threads (Open page in new window without popup blocking - too old and non-React-based, In ReactJS opening pdf in a new tab triggers browser's popup warning - didn't work) but they didn't help:

In React/NextJS I'm presenting a new browser tab with a PDF byte[] response generated on the server. Everything works and my response contains the byte array (verified).

const handlePrint = async (data: DataBean) => {

    // Create payload for POST from passed-in data object
    const payload = { ... };

    // This works
    const response = await post<any, typeof payload>("/create-pdf", payload);

    // Now present in a new browser tab
    const blob = new Blob([response], {type: "application/pdf;charset=utf-8"});
    const url = URL.createObjectURL(blob);
    window.open(url, '_blank');

The onClick source is

<a href="javascript:void(0)"  onClick={(e) => handlePrint(data)} title="Print PDF">Print PDF</a>}

There is a popup suppressed by the browser's popup blocker. I followed the 2-param window.open(url, '_blank') approach. There's also a blob with a URL.createObjectURL(blob) used to obtain the byte array result.

I read the similar threads (Open page in new window without popup blocking - too old and non-React-based, In ReactJS opening pdf in a new tab triggers browser's popup warning - didn't work) but they didn't help:

In React/NextJS I'm presenting a new browser tab with a PDF byte[] response generated on the server. Everything works and my response contains the byte array (verified).

const handlePrint = async (data: DataBean) => {

    // Create payload for POST from passed-in data object
    const payload = { ... };

    // This works
    const response = await post<any, typeof payload>("/create-pdf", payload);

    // Now present in a new browser tab
    const blob = new Blob([response], {type: "application/pdf;charset=utf-8"});
    const url = URL.createObjectURL(blob);
    window.open(url, '_blank');

The onClick source is

<a href="javascript:void(0)"  onClick={(e) => handlePrint(data)} title="Print PDF">Print PDF</a>}

There is a popup suppressed by the browser's popup blocker. I followed the 2-param window.open(url, '_blank') approach. There's also a blob with a URL.createObjectURL(blob) used to obtain the byte array result.

Share Improve this question edited Mar 10 at 18:14 gene b. asked Mar 10 at 18:07 gene b.gene b. 12.1k30 gold badges139 silver badges270 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

I figured it out. To avoid popup blockers, window.open has to be called BEFORE the actual response is written. Thus, this is working:

    // Immediately open a new tab to prevent popup blocking
    let newTab = window.open();

    // Get response
    const response = await post<any, typeof payload>("/create-pdf", payload);

    //... Other...: decoding from Base64, etc.

    const blob = new Blob([bytes], {type: 'application/pdf'});
    const url = URL.createObjectURL(blob);
    if (newTab) {
        newTab.location.href = url; 
    }

    

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论