Grails REST Web Services: Using custom Renderer to render PDF

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Grails REST Web Services: Using custom Renderer to render PDF

Eamonn O'Connell-2
Hi all,

I wonder if any one else has needed to facilitate application/pdf in their Rest api. 
Let's say I have an invoice, and I want to retrieve a pdf form of that invoice. 
The client could GET -H "Accept: application/pdf" /invoices/1

I wanted to make use of the new REST features in Grails, particularly the Custom Renderer. It doesn't seem to be possible though as PDFs are written with streams and are binary/byte in nature, while the Custom Renderer is expecting output to be character in nature and provides a writer to write the output.

It looks like I will have forgo the sexy stuff, and put the logic in my controller. 
Does anyone have any thoughts?

thanks,
Eamonn
Reply | Threaded
Open this post in threaded view
|

Re: Grails REST Web Services: Using custom Renderer to render PDF

Daniel Woods
The underlying implementation of RenderContext is ServletRenderContext, which you can use to get a handle on the Response object, and therein its output stream. To get the RendererRegistry to identify your renderer, you'll need to add the PDF MimeType to Config.groovy and inform your resource that 'pdf' (or whatever you called it) is a valid format. Code listed.

// grails-app/conf/Config.groovy
grails.mime.types [
    // ... [snip] ...
    pdf:           ['application/pdf']
]
grails.mime.use.accept.header true

//
@Resource(formats ['pdf'])
class Invoice {
    byte[bytes
    static constraints {
        bytes maxSizeInteger.MAX_VALUE
    }
}

//
class InvoiceRenderer implements Renderer<Invoice{
    final Class<InvoicetargetType Invoice
    final MimeType[mimeTypes [new MimeType("application/pdf")]

    @Override
    void render(Invoice objectRenderContext context{
        def ctx (ServletRenderContext)context
        def response ctx.webRequest.response
        response.contentType mimeTypes[0].name
        response.outputStream << object.bytes
    }
}


On Tue, Feb 25, 2014 at 6:20 AM, Eamonn O'Connell <[hidden email]> wrote:
Hi all,

I wonder if any one else has needed to facilitate application/pdf in their Rest api. 
Let's say I have an invoice, and I want to retrieve a pdf form of that invoice. 
The client could GET -H "Accept: application/pdf" /invoices/1

I wanted to make use of the new REST features in Grails, particularly the Custom Renderer. It doesn't seem to be possible though as PDFs are written with streams and are binary/byte in nature, while the Custom Renderer is expecting output to be character in nature and provides a writer to write the output.

It looks like I will have forgo the sexy stuff, and put the logic in my controller. 
Does anyone have any thoughts?

thanks,
Eamonn

Reply | Threaded
Open this post in threaded view
|

Re: Grails REST Web Services: Using custom Renderer to render PDF

Eamonn O'Connell-2
That worked a charm Daniel. Thanks!


On 25 February 2014 16:17, Daniel Woods <[hidden email]> wrote:
The underlying implementation of RenderContext is ServletRenderContext, which you can use to get a handle on the Response object, and therein its output stream. To get the RendererRegistry to identify your renderer, you'll need to add the PDF MimeType to Config.groovy and inform your resource that 'pdf' (or whatever you called it) is a valid format. Code listed.

// grails-app/conf/Config.groovy
grails.mime.types [
    // ... [snip] ...
    pdf:           ['application/pdf']
]
grails.mime.use.accept.header true

//
@Resource(formats ['pdf'])
class Invoice {
    byte[bytes
    static constraints {
        bytes maxSizeInteger.MAX_VALUE
    }
}

//
class InvoiceRenderer implements Renderer<Invoice{
    final Class<InvoicetargetType Invoice
    final MimeType[mimeTypes [new MimeType("application/pdf")]

    @Override
    void render(Invoice objectRenderContext context{
        def ctx (ServletRenderContext)context
        def response ctx.webRequest.response
        response.contentType mimeTypes[0].name
        response.outputStream << object.bytes
    }
}


On Tue, Feb 25, 2014 at 6:20 AM, Eamonn O'Connell <[hidden email]> wrote:
Hi all,

I wonder if any one else has needed to facilitate application/pdf in their Rest api. 
Let's say I have an invoice, and I want to retrieve a pdf form of that invoice. 
The client could GET -H "Accept: application/pdf" /invoices/1

I wanted to make use of the new REST features in Grails, particularly the Custom Renderer. It doesn't seem to be possible though as PDFs are written with streams and are binary/byte in nature, while the Custom Renderer is expecting output to be character in nature and provides a writer to write the output.

It looks like I will have forgo the sexy stuff, and put the logic in my controller. 
Does anyone have any thoughts?

thanks,
Eamonn