DEPRECATION WARNING

This documentation is not using the current rendering mechanism and is probably outdated. The extension maintainer should switch to the new system. Details on how to use the rendering mechanism can be found here.

EXT: PDF Generator 2

Author:Kasper Skårhøj
Created:2002-11-01T00:32:00
Changed by:Jens Ellerbrock
Changed:2006-04-28T00:58:01
Author:Jens Ellerbrock
Email:je@hades.org
Info 3:
Info 4:

EXT: PDF Generator 2

Extension Key: pdf_generator2

Copyright 2000-2002, Jens Ellerbrock, <je@hades.org>

This document is published under the Open Content License

available from http://www.opencontent.org/opl.shtml

The content of this document is related to TYPO3

- a GNU/GPL CMS/Framework available from www.typo3.com

Table of Contents

EXT: PDF Generator 2 1

Introduction 1

What does it do? 1

How does it work? 1

What is html2pdf? 1

File Based Caching 2

Users manual 2

Administration 2

Update .htaccess 2

Install the Extension 2

Available Options 2

Add a link in your Typoscript Template using makePdfLink 2

Add a link in your Typoscript Template using openPdfLink 3

Add a link in your Typoscript Template using getPdfTarget 3

Customizing the PDF-Page via Typoscript 3

Customizing the PDF-Page via the Constant Editor 4

Reference for tx_pdfgenerator2->makePdfLink, tx_pdfgenerator2->openPdfLink and tx_pdfgenerator2->getPdfTarget 4

Internals 5

gen_pdf.php 5

class.tx_pdfgenerator2.php 5

class.ux_tslib_fe.php 5

Known problems 6

To-Do list 6

Changelog 6

Introduction

What does it do?

If the user wants to print a document document served by typo3 he is usually offered a HTML page that contains only the relevant content of the page while the images are served asseparatefiles. This works fine for printing, but if the user wants to save the page for archiving or offline printing it would be nice to embedthe images within only one document. Adobe Acrobat PDF offers a suitable file format for thatpurposewhich is widely and os-independently supported.

This extension is based on the pdf_generator but uses a different html to pdf converter.

How does it work?

The extension generates a “printer-friendly” HTML-page internally. This page is passed through html2pdf to generatea PDFfile. ThePDFfile is then served to the user whilebeingcached by the usual caching mechanism of typo3.

The PDF page can be linked to from the Typoscript template using the stdWrap.postUserFunc parameter. Either dynamic Filenames (i.e. Index.php?id=12&type=123) or static (<alias>.PDF of 12.pdf) filenames are supported.

What is html2pdf?

Html2pdf s a 100% php based script that converts html including css to pdf (and alternatively to postscript which is not supported here). Unlike the htmldoc based solution css is supported and no external binaries are required.

It uses either the fpdf or the pdflib library to perform the rendering. Both methods give slightly different result both regarding rendering speed and rendering results. If you want to use pdflib you'll have to install additional fonts.

Supported content

Theoreticallythe PDF_generator 2 extension supports all content that can be rendered in HTML with css (i.e. all). It is however restricted by the html2pdf script it is using.

TemplaVoila Support

The extension supports templavoila by using the extension tv_pdfgen. See this extensions manual for a description on how to use templavoila with the pdf_generator2.

File Based Caching

If you do not want to store large generated files in the database, you can use the additional extension file_based_cache which will cache large files in the filesystem instead.

Users manual

The user should include an link to http://www.adobe.com/products/acrobat/readstep.html somewhere on the page to allow users to download the free acrobat reader if the do not have it already.

Not much else has to be done from a Users point of view. Simply click the link to the PDF File. The setup will determine whether the file will be opened in the browser window (inline) or in an external adobe acrobat viewer window (works onInternetexplorer, not on Netscape. Netscape will always open the PDF inline).

Administration

Install the Extension

Download the newest Extension using the Extension Manager/Extension Repository

Install the Extension using the Extension Manager

Configure the Extension.

install additional fonts for pdflib (sometime they are also needed for fpdf, i haven't found out when exactly yet. If you see an error message that a font can't be found, install them)

Update your .htaccess if you want to use “static” PDF filenames

You don't need to install the fpdf extension, the fpdf library is already included in this one.

Install additional fonts for PDFlib

If you want to use PDFlib you'll have to install additional fonts. This is most easily done by installing the pdf_generator2_fonts extension. They are packaged separately from the rest to keep the extension sizes manageable.

Update .htaccess

You will need to update your .htaccess if you want the links to your PDF files be static (i.e. <alias.pdf>). This change is necessaryto forward all requests to /<...>.PDF files to typo3's index.php.

ErrorDocument 404 /error.html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule   ^typo3$  /typo3/index_re.php
RewriteRule   ^[^/]*\.html$  index.php
RewriteRule   ^[^/]*\.pdf$  index.php

Available Options

simulateStaticPdf

This parameter will define whether the link to the PDF files will be a static one (i.e. <alias>.pdf) or dynamic ones (index.php?id=12&type=123). If you enable this you will need to update your .htaccess.

Disable gzip compression

If this parameter is set and you have gzip-compression turned on for the Frontend ( [FE][compressionLevel] in the installation tool) the generated PDF's will not be handled by the additional gzip- compression. It is highly recomended to leave this parameter at default (on) since PDF-files incorporate their own compression and you will only gain little additional compression. Additionally you may get problems with browsers that pass the stream to acrobat without decompressing it (e.g. Netscape 4.08).

typeID

This parameter defines the typeId that is used for the extension. Normally you will not need to change it.

Customizing the PDF-Page via Typoscript

The PDF is rendered based on a very simple template. You can easily update the pdf_generator page object to contain additional information or content elements on every PDF page. One especially interesting use is to add special HTML comments to control the behavior of the html2pdf conversion. See the html2pdf documentation (on http://www.tufat.com/docs/html2ps/index.html ) for details. A lot of parameters can be set via the Constant Editor (see next chapter).

:underline:`You don't have to put any of the following lines into your template, they are already there. They are noted here just for reference purposes.`

     pdf_generator = PAGE
 pdf_generator {
    typeNum = {$extension.pdf_generator.typeNum}
     config.pageGenScript = EXT:pdf_generator/gen_pdf.php
     config.admPanel = 0
      config.xhtml_cleaning = 0
config.USERNAME_substToken =
     config.ftu = 0
   config.disableCharsetHeader = 1
  config.prefixLocalAnchors = 0

   50 = CONTENT
     50 < styles.content.get
     }

Customizing the PDF-Page via the Constant Editor

You can set several options on a per-template basis with the Constant Editor. These options will result in parameters used for the html2pdf conversion. The following parameters can be set:

browserwidth

Property

browserwidth

Data type

string

Description

If set controlls the width of the page in pixels. Set this to control scaling of tables and pictures. It defaults to 700 which will give roughly the same resolution as pages printed from Firefox or Internet Explorer.

Default

700

size

Property

size

Data type

string

Description

Sets the size of the generated PDF. The size can be Letter, Legal, Executive, B5, Folio, A0Oversize and A0 through A10

Custom sizes are specified by the page width and length in mm separated by the letter "x" like 210x297

Default

A4

landscape

Property

landscape

Data type

boolean

Description

Set this to true if you want to generate the page in landscape mode

Default

False

top

Property

top

Data type

string

Description

Specifies the top margin in mm.

Default

13

bottom

Property

bottom

Data type

string

Description

Specifies the bottom margin in mm.

Default

13

left

Property

left

Data type

string

Description

Specifies the left margin in mm.

Default

13

pdfversion

Property

pdfversion

Data type

string

Description

Specifies the PDF version to render to.

Default

1.3

use_pdflib

Property

use_pdflib

Data type

boolean

Description

If this parameter is set the rendereing will be done with pdflib instead of fpdf. This will result in slightly different rendering results and times.

You'll need to install additional fonts if you want to use this.

Default

false

cssmedia

Property

cssmedia

Data type

String

Description

Css can contain several different medias. Here you can select which one to use.

Default

screen

renderfields

Property

renderfields

Data type

boolean

Description

Define whether fields are rendered editable. May not be supported with dlib depending on the pdflib version.

Default

true

Rederforms

Property

Rederforms

Data type

boolean

Description

Define whether forms are rendered as dynamic pdf forms. Only supported with fpdf.

Default

false

string_search[1-4]

Property

string_search[1-4]

Data type

string

Description

Using this parameter together with string_replace you can remove/replace certain parts of your html-file prior to passing it to the html2pdf conversion. One example is to remove the “Back” link in the recipe extension. You can do this (when using english language) with

string_search1 = “<A href="javascript:history.go(-1);">Back</A>”

and string_replace1 = “”

You can actually use string_search[5-n] as long as they are all set

Default

string_replace[1-4]

Property

string_replace[1-4]

Data type

string

Description

See string_search[1-4]

Default

regexp_search[1-4]

Property

regexp_search[1-4]

Data type

string

Description

The same as string_search, but for regular expressions. The search and replace parameters must be valid regular expressions that will be passed to preg_replace.

You can actually use regexp_search[5-n] as long as they are all set

Default

regexp_replace[1-4]

Property

regexp_replace[1-4]

Data type

string

Description

See regexp_search[1-4]

Default

Special Tags

You can force a page break by inserting any one of the following commands into the HTML page:

<!--NewPage-->
<pagebreak/>
<?page-break>

It is highly NOT RECOMMENDED to use these directives inside table cells, as you can get unpredictable results.

The following command performs a “section” break”.

<sectionbreak/>

Elements that are positioned fixed (i.e. on all pages), will only show up in the section they are declared in. Basically the body tag is split at the points where this command is found, the split up parts are then inserted in the original body tag again, thereby forming several new html-documents. If you keep this in mind, you'll also understand that tis command should only show up in places that are at the level below the body tag, otherwise you'll get unpredictable results

Special Fields

The following fields are rendered if the render fields checkbox in the constant editor is checked (default):

##PAGE##

Name

##PAGE##

Replaced with

Number of current page

##PAGES##

Name

##PAGES##

Replaced with

Total number of pages in the generated PDF file

##FILENAME##

Name

##FILENAME##

Replaced with

Filename (URL) of the source HTML

##FILESIZE##

Name

##FILESIZE##

Replaced with

Size of the source file (without included files)

##TIMESTAMP##

Name

##TIMESTAMP##

Replaced with

Date/time the PDF file have been generated

Internals

The extension consists of two php classes and one php script (plus a whole lot of classses that perform the actual html to pff conversion)

gen_pdf.php

This is the main script that generates the PDF file. This is done by calling the original pagegen script and then passing it to the html2pdf conversion script. The generated PDF is then returned to typo3 to be cached for subsequent requests. This script also “fixes” the following issues with the generated HTML:

Sometimes the RTE generates <P> tags within <PRE> tags. They are removed

When using buletted lists the output is theoretically correct but looks quite ugly (the dots are quite a bit above the text line. By changing the alignment of the images to left they look much better.

class.tx_pdfgenerator2.php

This class contains the makePdfLink function that is used to generate the link to the PDF file. It takes the current page id and all attached GET parameters and creates a new link using these values. If the simulateStaticPdf option is set these links will be named <alias>.pdf?<additional parameters>. if not they will be called index.php?id=<id>&type=<pdf_type>&<additional parameters>.

class.ux_tslib_fe.php

This class extends/overrides the core tslib_fe class. Two functions are overridden in typo3 <3.7. In typo3 3.7 and above callbacks are used instead:

checkAlternativeIdMethods

This change will check for a “.pdf” file ending and automatically generate the pageType for the PDF page (default:123)

processOutput

This change will check if a PDF file is currently generated and will add the additionally needed headers then. These include

  • Content-Type and Content-Dispositionsee Reference for postUserFunc = tx_pdfgenerator2->makePdfLink

  • Cache-control: private, Connection: Keep-AliveThese are need for Internet Explorer to allow downloadingof the file and to avoid IE requesting it twice.

  • Content-Length: <calculated length>

    This is also needed to avoid the need to press Refresh on IE when working with small PDF files.

Additionally the gzip compression is turned off here by setting the ['FE']['compressionlevel'] to ''.

Known problems

No proper check is done on parameters

((generated))

FAQ
  • Q: I see errors starting with:

  • MySQL server has gone away
    Warning: Cannot add header information - headers already sent by (output started at /local/typo3/httpd/htdocs/tslib/class.tslib_fe.php:1312)
    
  • A: This may happen when large pdf files are generated. The MySql Documentation recomends to set the max_allowed_packet=# value to a larger value.You can also use the extension file_based_cache in order to store large files in the file system instead of the database.

Troubleshooting

Things to do if anything fails:

  • Check the output for any clues
  • Check the html2pdf documentation for any clues: http://www.tufat.com/docs/html2ps/index.html
  • enable writing of the interim html data to a file by editing the pdfgen.php script. Search for the line// write the html for debugging puposesand replace the 'if(0)' with 'if(1)' in the line below.

To-Do list

  • Performance can probably be optimized a bit
  • Better parameter checking
  • Better debugging

Changelog

  • 0.0.1: - Initial Version, based on the pdf_generator 3.3.0
  • 0.1.0: -fixed usage of /tmp path
  • 0.1.1: - fixed some small bugs with the constant editor defaults- fixed another bug where images were not rendered properly- support fonts for pdflib in an additional package
  • 0.1.2: - fixed internal links
  • 0.2.0: - new version based on html2ps 1.9
  • 0.3.0: - new version based on html2ps 1.9.2, fix “getfontascender” bug
  • 0.3.1: - fixed bug in html2ps
  • 0.4.0: - fixed a bug concerning remotely linked (and “pseudo-remotely” linked via a <base..:> tag) files.
  • 0.5.0: - fields (pagenumbers etc. are now supported), - support for <sectionbreak/>

img-1 EXT: PDF Generator 2 - 7