When I tried to configure drupal service admin/structure/services/list/MyServiceName/server
, I only see the following response content types.
Is it possible to provide pdf, docx, pptx, xlsx etc. as response format?
Yes it's definitely possible, but you'll need to write a formatter class for each type of file as the bundled REST server only has support for XML, JSON, JSONP, serialized PHP, bencode, and optionally YAML.
To start, you need to implement hook_rest_server_response_formatters_alter
to let the REST server know about your new type, e.g.:
function MYMODULE_rest_server_response_formatters_alter(&$formatters) {
$formatters['pdf'] = array(
'mime types' => array('application/pdf'),
'formatter class' => 'MyModuleServicesPDFFormatter',
);
}
Then add your formatter class, which needs to implement ServicesFormatterInterface
and provide the render
method:
class MyModuleServicesPDFFormatter implements ServicesFormatterInterface {
public function render($data) {
return some_function_to_convert_data_to_pdf_string($data);
}
}
Then make sure that class is auto-loaded by putting this in your module's .info file:
files[] = relative/path/to/MyModuleServicesPDFFormatter.php
Repeat the process for all of the new formatters you wish to provide, clear the cache when you're done.