Search code examples
wordpresswordpress-rest-api

How to return binary data from custom wordpress rest api endpoint


I am writing a custom endpoint for a REST api in wordpress, following the guide here: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/

I am able to write a endpoint that returns json data. But how can I write an endpoint that returns binary data (pdf, png, and similar)?

My restpoint function returns a WP_REST_Response (or WP_Error in case of error). But I do not see what I should return if I want to responde with binary data.


Solution

  • I would look at something called DOMPDF. In short, it streams any HTML DOM straight to the browser. We use it to generate live copies of invoices straight from the woo admin, generate brochures based on $wp_query results etc. Anything that can be rendered by a browser can be streamed via DOMPDF.