In this post we will perform similar steps in order to create a PDF version of the “Product List” screen.
PDF exports are supported out of the box by the OFBiz widgets.
In this excercise we will:
- add to the product list screen a link, close to the CSV export link we have created in our last post, that will generate the PDF export
- add to the controller.xml file the request and view entries for the new PDF export screen
- implement the new screen definition for the PDF export screen; the screen will reuse the same form used by html screen but with a different decorator (more suited for PDF output)
Adding a link element
We have already learned how to create a link in a screen. We add it after the “CSV Export” link:
<container style="button-bar"> <link target="ProductListExport" text="CSV Export" style="buttontext"/> <link target="ProductListPDF" text="PDF Export" style="buttontext"/> </container>
- the new link is inside of the same “container” element of the “CSV Export” link; we want the two links rendered close to each other
- the link element is defined in the same way we have defined the other link
Controller entries for a PDF export screen
<request-map uri="ProductListPDF"> <security https="true" auth="true"/> <response name="success" type="view" value="ProductListPDFScreen"/> </request-map> ... <view-map name="ProductListPDFScreen" type="screenfop" content-type="application/pdf" page="component://hwm/widget/HwmScreens.xml#ProductListPDF"/>
The entries are very similar to the ones we have implemented in a previous post for the product list screen. There are just a couple of things to notice:
- as usual, the request-map uri must match with the target of the link element in the screen (“ProductListPDF”)
- in the view-map, the type is now “screenfop” (instead of “screen”) because we have to invoke the “fop screen renderer” to generate a PDF output
- in the view-map we have also set the content-type to “application/pdf” as useful metadata information for the browser
Screen definition for PDF export screens
There isn’t anything special in a screen definition for a PDF export; the screen is defined in the same way of a normal screen, except for a couple of details:
<screen name="ProductListPDF"> <section> <actions> <set field="viewSize" value="10000"/> <set field="pageLayoutName" value="main-page-landscape"/> <entity-condition entity-name="Product" list="products"> <order-by field-name="productId"/> </entity-condition> </actions> <widgets> <decorator-screen name="FoReportDecorator" location="component://common/widget/CommonScreens.xml"> <decorator-section name="body"> <container> <label text="Finished Products" style="h1"/> </container> <include-form name="ListProducts" location="component://hwm/widget/HwmForms.xml"/> </decorator-section> </decorator-screen> </widgets> </section> </screen>
- the screen definition is mostly identical to the “product list screen” except for the decorator element: we are now using the FoReportDecorator that is one of the screen decorators available out of the box in OFBiz (it renders simple PDF reports with a page-of-pages footer, logo etc.)
- in particular, the entity-condition element is the same and it is used to select the list of products for the export
- the form that is included is the same of the “product list screen”: we will not have to re-implement it because the widget renderer will take care of rendering it into the proper output (PDF or CSV or html)
- as we already did for the CSV screen, the “set” field operation, where we set “viewSize” to “10000”, is an easy way to “disable” pagination; we actually define the top limit of our product export to 10000 records here, but of course we can use a different value
- there is an additional set operation here (for “pageLayoutName”), to set the PDF layout to landscape
The exercise is complete and we can test the product PDF by clicking the “PDF Export” link (there is no need to restart OFBiz).
Implementing the PDF report ended up being very similar to implementing the CSV export (or HTML screen): in OFBiz the same patterns, tools and languages can be reused to dramatically improve productivity.