• I’m integrating a wordpress site with an external API. I have a form that posts to my server and I call a function on my server that makes a wp_remote_get call to the external API.

    The external API returns a PDF, with 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 the response body is the PDF in nasty string format, which seems like a good start.

    How do I pass this PDF to the user’s browser? ie,

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

    I have to hit my server first, cannot post the form directly to the external API.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    This needs to be in response to a browser request, you cannot push a file download. First send the same content-type header using header(), then tell the browser what the intended filename is with
    header("Content-Disposition:attachment;filename='downloaded.pdf'");

    Follow with the same data stream using echo. You’re basically relaying the API response verbatim.

Viewing 1 replies (of 1 total)
  • The topic ‘Send pdf from wp_remote_get to browser’ is closed to new replies.