Can i display a WepPage in a PDF (How to get the rendered markup)?

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

Can i display a WepPage in a PDF (How to get the rendered markup)?

Per Newgro
Hi,

i would like to render a WebPage with flying saucer (PDF generator).
I've created a resource reference and a ByteArrayResource.

But now i need the rendered markup of the page (e.g. HomePage).

So my question is how can i render the markup in my Resource?

Thanks for your support
Per

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

Per Newgro
Found the solution already. I did it that way:

public class FlyingSaucerPdfResource extends ByteArrayResource {

     public FlyingSaucerPdfResource() {
         super("application/pdf");
     }

     @Override
     protected byte[] getData(Attributes attributes) {
         ByteArrayOutputStream os;
         try {
             CharSequence buf = renderPage(HomePage.class,
attributes.getParameters());
             ITextRenderer renderer = new ITextRenderer();
             renderer.setDocumentFromString(buf.toString());
             renderer.layout();
             renderer.createPDF(os = new ByteArrayOutputStream());
             os.close();
         } catch (IOException | DocumentException e) {
             throw new RuntimeException(e);
         }
         return os.toByteArray();
     }

     private CharSequence renderPage(final Class<? extends Page> pageClass,
             PageParameters parameters) {

         final RenderPageRequestHandler handler = new
RenderPageRequestHandler(
                 new PageProvider(pageClass, parameters),
                 RedirectPolicy.NEVER_REDIRECT);

         final PageRenderer pageRenderer = getApplication()
                 .getPageRendererProvider().get(handler);

         RequestCycle originalRequestCycle = RequestCycle.get();

         BufferedWebResponse tempResponse = new BufferedWebResponse(null);

         RequestCycleContext requestCycleContext = new RequestCycleContext(
                 originalRequestCycle.getRequest(), tempResponse,
                 getApplication().getRootRequestMapper(), getApplication()
                         .getExceptionMapperProvider().get());
         RequestCycle tempRequestCycle = new
RequestCycle(requestCycleContext);

         final Response oldResponse = originalRequestCycle.getResponse();

         try {
             originalRequestCycle.setResponse(tempResponse);
             pageRenderer.respond(tempRequestCycle);
         } finally {
             originalRequestCycle.setResponse(oldResponse);
         }

         return tempResponse.getText();
     }

     private Application getApplication() {
         return Application.get();
     }

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

Martin Grigorov-4
Hi,

Since Wicket 6.7.0 there is ComponentRenderer class which does exactly the
same.


On Sat, Jun 8, 2013 at 12:01 AM, Per Newgro <[hidden email]> wrote:

> Found the solution already. I did it that way:
>
> public class FlyingSaucerPdfResource extends ByteArrayResource {
>
>     public FlyingSaucerPdfResource() {
>         super("application/pdf");
>     }
>
>     @Override
>     protected byte[] getData(Attributes attributes) {
>         ByteArrayOutputStream os;
>         try {
>             CharSequence buf = renderPage(HomePage.class,
> attributes.getParameters());
>             ITextRenderer renderer = new ITextRenderer();
>             renderer.**setDocumentFromString(buf.**toString());
>             renderer.layout();
>             renderer.createPDF(os = new ByteArrayOutputStream());
>             os.close();
>         } catch (IOException | DocumentException e) {
>             throw new RuntimeException(e);
>         }
>         return os.toByteArray();
>     }
>
>     private CharSequence renderPage(final Class<? extends Page> pageClass,
>             PageParameters parameters) {
>
>         final RenderPageRequestHandler handler = new
> RenderPageRequestHandler(
>                 new PageProvider(pageClass, parameters),
>                 RedirectPolicy.NEVER_REDIRECT)**;
>
>         final PageRenderer pageRenderer = getApplication()
>                 .getPageRendererProvider().**get(handler);
>
>         RequestCycle originalRequestCycle = RequestCycle.get();
>
>         BufferedWebResponse tempResponse = new BufferedWebResponse(null);
>
>         RequestCycleContext requestCycleContext = new RequestCycleContext(
>                 originalRequestCycle.**getRequest(), tempResponse,
>                 getApplication().**getRootRequestMapper(),
> getApplication()
>                         .getExceptionMapperProvider().**get());
>         RequestCycle tempRequestCycle = new RequestCycle(**
> requestCycleContext);
>
>         final Response oldResponse = originalRequestCycle.**getResponse();
>
>         try {
>             originalRequestCycle.**setResponse(tempResponse);
>             pageRenderer.respond(**tempRequestCycle);
>         } finally {
>             originalRequestCycle.**setResponse(oldResponse);
>         }
>
>         return tempResponse.getText();
>     }
>
>     private Application getApplication() {
>         return Application.get();
>
>     }
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<[hidden email]>
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

harmoniaa
FYI:

I had a problem using ComponentRenderer with ListView (it was complaining the there was no markup for wicket-id "my-wicket-id").

Additionally, ITextRenderer (or xerces actually) was not able to parse XML from the markup with Wicket tags ("The prefix "wicket" for element "wicket:container" is not bound.").

I found another solution here: http://stackoverflow.com/a/11898702/536265

Then I added this before rendering:

  IMarkupSettings markupSettings = component.getApplication().getMarkupSettings();
  boolean originalStripWicketTags = markupSettings.getStripWicketTags();
  markupSettings.setStripWicketTags(true);

And to the finally block:

  markupSettings.setStripWicketTags(originalStripWicketTags);

If you think ComponentRenderer could/should be improved, maybe this will give you some ideas.
Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

Martin Grigorov-4
Hi Edvard,

Thank you for the feedback.

ComponentRenderer (CR) just asks Wicket to render a component. The
rendering process uses all the application settings so if the wicket
tags/attributes cause any problems then the application can turn them off
before using CR and turn on after.
So I think CR doesn't need to be changed to have this code in itself.

In production mode Wicket strips these tags and attributes unless you
explicitly turn them on.


On Mon, Jun 10, 2013 at 4:05 PM, harmoniaa <
[hidden email]> wrote:

> FYI:
>
> I had a problem using ComponentRenderer with ListView (it was complaining
> the there was no markup for wicket-id "my-wicket-id").
>
> Additionally, ITextRenderer (or xerces actually) was not able to parse XML
> from the markup with Wicket tags ("The prefix "wicket" for element
> "wicket:container" is not bound.").
>
> I found another solution here: http://stackoverflow.com/a/11898702/536265
>
> Then I added this before rendering:
>
>   IMarkupSettings markupSettings =
> component.getApplication().getMarkupSettings();
>   boolean originalStripWicketTags = markupSettings.getStripWicketTags();
>   markupSettings.setStripWicketTags(true);
>
> And to the finally block:
>
>   markupSettings.setStripWicketTags(originalStripWicketTags);
>
> If you think ComponentRenderer could/should be improved, maybe this will
> give you some ideas.
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Can-i-display-a-WepPage-in-a-PDF-How-to-get-the-rendered-markup-tp4659290p4659352.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

harmoniaa
Good point. Anyway, that still leaves me the issue with ListView...
Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

Martin Grigorov-4
On Mon, Jun 10, 2013 at 4:30 PM, harmoniaa <
[hidden email]> wrote:

> Good point. Anyway, that still leaves me the issue with ListView...
>

Please create a ticket with a quickstart for this.


>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Can-i-display-a-WepPage-in-a-PDF-How-to-get-the-rendered-markup-tp4659290p4659354.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

harmoniaa
I created a quickstart but didn't create a ticket yet, because I found out that ComponentRenderer expects to get a component whose markupId equals to ComponentRenderer.COMP_ID.

Is this a bug or a feature? Is there some way to render the markup of an existing component that has an arbitrary markupId using ComponentRenderer?

I tried calling #setMarkupId and wrapping the existing component to a new component with expected markupId but both ways failed. Would it be easy to enhance ComponentRenderer to work with any markupIds?
Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

Martin Grigorov-4
Hi,

This can be relaxed. Please file a ticket.


On Wed, Jun 12, 2013 at 2:55 PM, harmoniaa <
[hidden email]> wrote:

> I created a quickstart but didn't create a ticket yet, because I found out
> that ComponentRenderer expects to get a component whose markupId equals to
> ComponentRenderer.COMP_ID.
>
> Is this a bug or a feature? Is there some way to render the markup of an
> existing component that has an arbitrary markupId using ComponentRenderer?
>
> I tried calling #setMarkupId and wrapping the existing component to a new
> component with expected markupId but both ways failed. Would it be easy to
> enhance ComponentRenderer to work with any markupIds?
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/Can-i-display-a-WepPage-in-a-PDF-How-to-get-the-rendered-markup-tp4659290p4659415.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Can i display a WepPage in a PDF (How to get the rendered markup)?

harmoniaa