wordpress - Send pdf from wp_remote_get to browser -


i'm integrating wordpress site external api. have form posts server , call function on server makes wp_remote_get call external api.

the external api returns pdf, headers like:

        [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 

and response body pdf in nasty string format, seems start.

how pass pdf user's browser? ie,

$response = wp_remote_get( $url, array ( //stuff request needs)); if (is_wp_error ($response)) {     $error_message = $response->get_error_message();     echo "something went wrong: $error_message"; } else {     //what here? } 

i have hit server first, cannot post form directly external api.

i managed using javascript redirect form submit new window on site, passing along form information url params. ie, in html:

<form onsubmit="return qrsdownload()"> 

and in javascript:

function qrsdownload() {     // bunch of jquery , processing build url..     window.open(url, 'my title', 'width=800, height=600'); } 

the page opened single-use page template created, omitted standard wp templates in (so no header, no footer, no wordpress loop), , in page's php file:

<?php  if (isset($_get['someparam'])) {   // started off logic verify had params needed   // because jump directly page } $url = "whateverurl.com/myendpoint"; // additional logic set api call $server_response = wp_remote_get($url, $args); if (is_wp_error( $server_response) ) {     // handle error } else {     // pass our pdf 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(); }  ?> 

the approach pass result api straight out user, need headers specific it's pdf, , headers have set before write out output. easier way ensure in new window, rather strictly on form postback.


Comments

Popular posts from this blog

angular - DownloadURL return null in below code -

meteor - inserting data to database gives error "insert failed: Method '/texts/insert' not found" -