You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Per Newgro <pe...@gmx.ch> on 2013/06/07 22:21:50 UTC

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

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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


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

Posted by harmoniaa <ed...@nitorcreations.com>.
https://issues.apache.org/jira/browse/WICKET-5232



--
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-tp4659290p4659438.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


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

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

This can be relaxed. Please file a ticket.


On Wed, Jun 12, 2013 at 2:55 PM, harmoniaa <
edvard.fonsell@nitorcreations.com> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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

Posted by harmoniaa <ed...@nitorcreations.com>.
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


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

Posted by Martin Grigorov <mg...@apache.org>.
On Mon, Jun 10, 2013 at 4:30 PM, harmoniaa <
edvard.fonsell@nitorcreations.com> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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

Posted by harmoniaa <ed...@nitorcreations.com>.
Good point. Anyway, that still leaves me the issue with ListView...



--
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


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

Posted by Martin Grigorov <mg...@apache.org>.
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 <
edvard.fonsell@nitorcreations.com> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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

Posted by harmoniaa <ed...@nitorcreations.com>.
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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


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

Posted by Martin Grigorov <mg...@apache.org>.
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 <pe...@gmx.ch> 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<us...@wicket.apache.org>
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

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

Posted by Per Newgro <pe...@gmx.ch>.
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
For additional commands, e-mail: users-help@wicket.apache.org