With the Views PDF module you can output a view as a PDF document. Each field of the view can be placed on the PDF page directly in the administration interface. Therefore a new display called "PDF Page" is added.
There are already some PDF solutions such as Print. But these solutions use the HTML output and converts this to PDF. The disadvantages of such an integration are:
- No control over page flow (e.g. page break).
- Little or no control over page header and footer.
- You need HTML skills to change the layout.
- The rendering is slow and need a lot of memory, because it need to render the HTML.
- Complex tables make troubles.
- Vector graphics can not be implemented, therefore the printing of the document can be problematic.
- You are limited by HTML's capabilities.
Installation
Installation instructions:
- Download the module or checkout the module.
- Upload the module to your Drupal instance.
- Download the required libraries. Download TCPDF and FPDI.
Recomended versions: FPDI 1.4.4, TCPDF 6.0.020, FPDF_TPL 1.2.3
- Copy the files to "sites/all/libraries/" directory in the Drupal directory into their own "fpdi" and "tcpdf" sub-directories and include "fpdf_tpl.php" in the "fpdi" sub-directory.
- Set up a view with a PDF display.
- Use it.
About jQuery Update
jQuery Update is only required for Views UI to upload templates. It only needs to be set to version 1.5 or higher.
As of jQuery Update 7.x-2.5 the jQuery version can be set as low as 1.4.4, the default version for Drupal 7. Also, admin pages can be set to use a different version. Views PDF only needs the admin pages to be set to 1.5 or higher. Once your views are created, you can change the settings to whatever version gives you best compatibility with the other modules on your site.
Basic Usage
- Create a new view or use an existing view. New to views? See the documentation of the Views module.
- In the view add the display "PDF Page".
- Select the new added display.
- Select the Style. You can use PDF unformatted to place the fields in no structured way on the PDF. Use PDF table to place the fields in table with a table header.
- In the settings of the style you can set up per field settings such as the position, the size of the field, the font and so on. Important: Switch to the "PDF Page" display in order to see these options; they don't appear in the "Defaults" display.
- Under PDF Page Setting you can set up the size of the page.
- Under PDF Font Settings you can set up the default fonts for the PDF.
- Under PDF Template Settings you can set up a background PDF.
- To add a page break, you can add a PDF page break field. When this field is rendered new page is added. Reorder the fields if necessary.
- You can find also a page number field. You can use it to print the current page number. To position the field in the header or footer, set the relative position in the field settings to "In header / footer".
Unformatted Style
Positions
You can position fields relative to
- The page: The field is positioned to the page left top, left upper, right bottom or left bottom corner. Depending what you select by the corner setting.
- The footer / header: The field will be repeated automatically in the footer / header.
- The last writing position: The position of the writing head will be unchanged.
- Itself: The first time the field is written to the PDF, the last writing position is used. (used for multiple nodes)
- Another field: You can select any earlier written field to use as the relative position.
All these positions can be modified by adding a x resp. y coordinate. These coordinates were added to the position selected by the above relative position. The default unit for these coordinates is millimeters, but that can be changed under "PDF Page Settings" (choose between point, centimeter and inch). In the fields themselves, just write numbers (no unit).
Text Settings
Here you can select the text settings for the field. This settings overwrites the default values, which you can specify under "PDF Fonts Settings". Here you can read how you can create additional fonts. Place the font files in the directory "lib/tcpdf/fonts".
Render Settings
In the render settings fields you can add additional settings which were not covered by the GUI settings above. With "$this" you can access the TCPDF API. Additional there are the following variables:
$w = // width
$h = // height
$border = // border option, see the TCPDF documentation about this option
$align = // the alignment
$fill = // fill the cell acceptable values are true or false, use $this->SetFillColor() on next line to set color of fill
$ln = // position after
$reseth = // reset the hight
$stretch = // character stretch
$ishtml = // html is rendered / or not
$autopadding = // set the padding automatically
$maxh = // set max height
$valign = // vertical alignment
$fitcell = // reduce the font size if the text does not match into it
$content = // The content of the field
For example this can be used to transform the fields text to uppercase:
$content = strtoupper($content);
You have the option to use the API before the field is printed out with MultiCell and after the field is printed. Normally you can use the second one to reset the previous changes.
Important: The entered code must be valid PHP code. Note that in the PHP Code fields the <?php and the ?> delimiters are not needed.
Table Views Style
To print a table in the PDF you can use the table view style. There you can specify for each column a header formatting and the table body formatting of this field. The options are very similar to the unformatted options. In contrast to the unformatted style, you cannot position the fields complete free on the PDF, you can only set the row height and the width of the cell.
But the table itself can be positioned totally free on the PDF. If the page end is reached, a page break is automatically inserted.
Hyphenation
The hyphenation is implemented by the TCPDF library. To use hyphenation you need to download the Tex patterns and save them in "hyphenate_patterns" in the TCPDF library folder. The patterns can be found here.
PDF Templates
Normally you want also to print static content on the PDF such as a company logo or an address. To simplify this process a so called PDF template is added. This is a normal PDF file which contains all the static content. The dynamic content (view fields) will be printed on this PDF.
You can setup such a file per row or for the whole page. The per row file make sense if you have for example for each node a different background or if you want to combine multiple backgrounds. The background could be also a file field. This enables the option to change the background per row.
Additionally you can setup also a leading and a succeed template file. This pages will be added without printing on it. This is useful if you want to add the general terms and conditions in your PDF invoice.
Append and Include other Views
There are two additional modules included in the Views PDF module. One is to append a view and the other is to include a view. Append means that the referenced view is appended to the current view. Include means that the referenced view will be added as a field to the view.
With these modules you can implement much more complex PDF's. For example if you want to add the invoice lines to your invoice it can be hard to implement this by a single query. If you split this in a "invoice" query and in a "invoice line" query, the task becomes very easy. You can add the invoice lines as a table in a separate view trough the include field module. See the next paragraph for more details about this.
Use Case 1: Invoice for Ubercart
A typical use case for PDF's is the invoice. Most customers especially in B2B want a PDF invoice. Ubercart does not provide such a invoice. There exists a module for invoice generating. The approach of this module is to generate from HTML the PDF with DOMPDF. This is a nice library, but consumes much RAM and it takes some time to generate the PDF. Additionally you can control the layout only be HTML. It is also not integrated with Ubercart.
The module Ubercart Views provides an interface to the Ubercart order and order lines. In combination with this module you can create directly PDF invoices. Therefore you need to add a new order view. This builds the main invoice. There you setup the address, the invoice header (order id, order date ...) and the order footer.
You can then add an order line view with the PDF table style. This order line view can then be included in the invoice view.
The Ubercart PDF Invoice project is implemented as above described.
Use Case 2: Print at Home Solution
A common problem with the Signup module is, that you want providing a ticket as PDF for your customers. This module can help you to produce such a PDF.
Here you can find a demonstration of the use case:
http://pdf.use-cases.customweb.ch/
This use case consists of the Signup, Ubercart, Ubercart Signup Integration and some custom code (integration of Ubercart Signup with the Views module).
The approach is very similar to the first use case. If you can access the data with a view, then you can generate the PDF.
You setup a new view with the "signup" entity as the base. Then you join all the relevant information to this entity. Add the required fields and put it on the PDF.
Troubleshooting empty pages when including other Views
If Views PDF is used with an inserted view and that view is the last field before a page break, then the page break should be aligned "top left" to the "Page." Otherwise, it can happen that either empty pages or an overflow of the included view to the next page occur.
In case of not using any page breaks, but the inserted view is still the last field on the page and an empty page occurs at the end of the PDF, aligning the Inserted View field to "Last writing position" instead of the previous field may help.