Wordpress 将pdf从wp_remote_发送到浏览器

Wordpress 将pdf从wp_remote_发送到浏览器,wordpress,pdf,Wordpress,Pdf,我正在将wordpress站点与外部API集成。我有一个表单发布到我的服务器,我在服务器上调用一个函数,该函数对外部API进行wp_remote_get调用 外部API返回一个PDF,其标题如下: [date] => Fri, 18 Aug 2017 15:59:19 GMT [x-powered-by] => Servlet/3.0 [cache-control] => no-cache [content-typ

我正在将wordpress站点与外部API集成。我有一个表单发布到我的服务器,我在服务器上调用一个函数,该函数对外部API进行wp_remote_get调用

外部API返回一个PDF,其标题如下:

        [date] => Fri, 18 Aug 2017 15:59:19 GMT
        [x-powered-by] => Servlet/3.0
        [cache-control] => no-cache
        [content-type] => application/pdf;charset=utf-8
        [content-language] => en-US
响应主体是令人讨厌的字符串格式的PDF,这似乎是一个好的开始

如何将此PDF传递到用户的浏览器? 即


我必须先点击我的服务器,不能将表单直接发布到外部API。

我通过使用javascript将表单提交重定向到我网站上的一个新窗口,并将表单信息作为URL参数传递,从而实现了这一点。 即, 在我的HTML中:

<form onsubmit="return qrsDownload()">
我打开的页面是我创建的一次性页面模板,我省略了其中的标准WP模板(因此没有页眉,没有页脚,没有wordpress循环),并且在该页面的php文件中:

<?php 
if (isset($_GET['someParam'])) {
  // started off with logic to verify that I had the params needed
  // because anybody could just jump directly to this page now
}
$url = "whateverurl.com/myendpoint";
// additional logic to set up the API call
$server_response = wp_remote_get($url, $args);
if (is_wp_error( $server_response) ) {
    // do something to handle error
} else {
    // PASS OUR PDF to the user
    $response_body = wp_remote_retrieve_body( $server_response);
    header("Content-type: application/pdf");
    header("Content-disposition: attachment;filename=downloaded.pdf");
    echo $response_body;
}
} else {
     get_header();
    $html = "<div class='content-container'><p>A pretty error message here.</p></div>";
     echo $html;
     get_footer();
}

?>

function qrsDownload() {
    // a bunch of jquery and processing to build the URL..
    window.open(url, 'My Title', 'width=800, height=600');
}
<?php 
if (isset($_GET['someParam'])) {
  // started off with logic to verify that I had the params needed
  // because anybody could just jump directly to this page now
}
$url = "whateverurl.com/myendpoint";
// additional logic to set up the API call
$server_response = wp_remote_get($url, $args);
if (is_wp_error( $server_response) ) {
    // do something to handle error
} else {
    // PASS OUR PDF to the user
    $response_body = wp_remote_retrieve_body( $server_response);
    header("Content-type: application/pdf");
    header("Content-disposition: attachment;filename=downloaded.pdf");
    echo $response_body;
}
} else {
     get_header();
    $html = "<div class='content-container'><p>A pretty error message here.</p></div>";
     echo $html;
     get_footer();
}

?>