The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Wx::PdfDocument - wxPerl wrapper for the wxPdfDocument classes.


    use Wx::PdfDocument;
    use Wx wq( :pdfdocument );
    my $pdfdoc = Wx::PlPdfDocument->new;
    my $pdfdoc2 = MyDocClass->new();
    package MyDocClass;
    use strict;
    use warnings;
    use Wx::PdfDocument;
    use Wx wq( :pdfdocument );
    use base qw( Wx::PlPdfDocument );
    sub new { shift->SUPER::new( @_ ); }
    # optionally override virtuals
    sub Header { .... }
    sub Footer { .... }
    sub AcceptPageBreak { .... }


Wx::PdfDocument is a wrapper for the wxPdfDocument wxcode classes for the wxWidgets GUI toolkit. Wx::PdfDocument allows you to create PDF documents directly using Wx::PlPdfDocument methods either directly or in a derivied class. It also provides classes for the wxWidgets printing framework that allow you to print output to a PDF document using any Wx::Printout class. For example, you can print the output of Wx::RichTextPrintout and Wx::HtmlPrintout in addition to any custom Wx::Printout that you may create.

A module is provided in this distribtion for Wx::Demo that gives extensive examples of usage.


For creating PDF documents with your own code, this may be the only class you will need to use. You can either us the class directly or create a derived class and optionally override the following virtual methods.


The interface is fully described at the wxPdfDocument documentation site.

As is the normal practice for wxPerl implementations, where the C++ documentation indicates any type of an array parameter, you should pass a reference to a Perl array containing the appropriate types or objects.

A small number of methods cannot be implemented exactly as they are implemented in C++. The wxPerl implementation for these methods is noted below:

    my($x, $y, $width, $height) = $pdfdoc->GetTemplateBBox($templateId);
    my($newWidth, $newHeight) = $pdfdoc->GetTemplateSize($templateId, $oldWidth, $oldHeight);


The distribution provides a Wx::DC class that you can use to write a PDF document using the standard Wx::PrinterDC commands.

    my $pdata = Wx::PrintData->new;
    my $dc = Wx::PdfDC->new( $pdata );
    ... do drawing / writing

The interface is described at the wxPdfDocument documentation site but is a near complete implementation of Wx::DC.

wxWidgets Printing Framework

Classes are provided that allow you to use Wx::PdfDocument with existing wxWidgets classes in the manner of a PDF printer.

For example, with Wx::RichextPrintout

    my $printdata;
    my $dialogdata = Wx::PageSetupDialogData->new;
    my $dialog = Wx::PdfPageSetupDialog->new($self, $dialogdata);
    if($dialog->ShowModal == wxID_OK ) {
        $dialogdata = $dialog->GetPageSetupDialogData;
        $printdata  = Wx::PdfPrintData->new($dialogdata->GetPrintData);
    return unless $printdata;
    my $printbuffer = Wx::RichTextBuffer->new($self->{richtext}->GetBuffer);
    my $printprintout = Wx::RichTextPrintout->new("Demo RichText PDF Printing");
                      10 * $dialogdata->GetMarginTopLeft->y,
                      10 * $dialogdata->GetMarginBottomRight->y,
                      10 * $dialogdata->GetMarginTopLeft->x,
                      10 * $dialogdata->GetMarginBottomRight->x
    my $previewbuffer = Wx::RichTextBuffer->new($self->{richtext}->GetBuffer);
    my $previewprintout = Wx::RichTextPrintout->new("Demo RichText PDF Preview");
                      10 * $dialogdata->GetMarginTopLeft->y,
                      10 * $dialogdata->GetMarginBottomRight->y,
                      10 * $dialogdata->GetMarginTopLeft->x,
                      10 * $dialogdata->GetMarginBottomRight->x
    # Printouts do not take ownership of buffers so the
    # wxWidgets buffers will be deleted along with our Perl
    # objects. The preview frame is not a modal dialog.
    $self->{storerichtextbuffers} = [ $printbuffer, $previewbuffer ];
    my $printpreview = Wx::PdfPrintPreview->new( $previewprintout, $printprintout, $printdata);
    my $frame = Wx::PreviewFrame->new( $printpreview, $self,
                                     "PDF RichText Printing Preview", [-1, -1], [600, 600] );
    $frame->Show( 1 );


This page setup dialog allows you to collect settings for margins, paper type and orientation from the user. It uses the standard Wx::PageSetupDialogData that you can use to determine which of the three settings; margins, paper and orientation, will be available to the user to change.

    # get margins and paper but don't let user
    # change orientation
    my $printdata;
    my $dialogdata = Wx::PageSetupDialogData->new;
    my $dialog = Wx::PdfPageSetupDialog->new($self, $dialogdata);
    if($dialog->ShowModal == wxID_OK ) {
        $dialogdata = $dialog->GetPageSetupDialogData;
        $printdata  = $dialogdata->GetPrintData;


This print dialog takes the place of the standard printer setup dialog when using the wxWidgets printing framework. It is shown by default when using Wx::PdfPrinter and Wx::PdfPrintPreview and you may also create a dialog yourself to collect user input.

The dialog allows the user to set document properties such as title and subject, choose the output filename and whether to launch the output in a PDF viewer. It also allows setting encryption and document passord options.

You can control which of the options are available to the user by passing a Wx::PdfPrintData object in the constructor with appropropriate settings.

    # all options available
    my $printdata = Wx::PdfPrintData->new;
    my $dialog = Wx::PdfPrintDialog->new($self, $printdata);
    if($dialog->ShowModal == wxID_OK ) {
        $printdata  = $dialog->GetPdfPrintData;
    # limit options to filepath and launch viewer
    my $printdata = Wx::PdfPrintData->new;
    ## check launch doc CheckBox by default
    my $dialog = Wx::PdfPrintDialog->new($self, $printdata);
    if($dialog->ShowModal == wxID_OK ) {
        $printdata  = $dialog->GetPdfPrintData;
        Wx::LogMessage('Selected filepath : %s', $printdata->GetFilename);


This class provides most of the custom features for wxPdfPrinting and is the basis for passing options to and from Wx::PdfPrintDialog, Wx::PdfPrinter and Wx::PdfPrintPreview.


    my $pdata = Wx::PdfPrintData->new();
    my $pdata = Wx::PdfPrintData->new(<Wx::PdfPrintData>);
    my $pdata = Wx::PdfPrintData->new(<Wx::PrintData>);
    my $pdata = Wx::PdfPrintData->new(<Wx::PrintDialogData>);

PDF Document Properties

Set the output filename and basic paper detail

    $filename = $pdata->GetFilename;
    $paperid = $pdata->GetPaperid;
    $orient = $pdata->GetOrientation;

You can set various PDF Document properties by adding the information to Wx::PdfPrintData

    $title = $pdata->GetDocumentTitle();
    $subject = $pdata->GetDocumentSubject();
    $author = $pdata->GetDocumentAuthor();
    $keywords = $pdata->GetDocumentKeywords();
    $applicationname = $pdata->GetDocumentCreator();

Document Protection

see wxPdfDocument::SetProtection described at

Normally you should collect the options for document protection via the Wx::PdfPrintDialog and let the Wx::PdfPrinter class apply these options for you. To set and apply options directly yourself to a PDF document it is better to call Wx::PdfDocument methods directly.

The following methods of Wx::PdfPrintData are used internally by the Wx::PdfPrinter

    $pdata->SetDocumentProtection($perms, $userpwd, $ownpwd, $cryptm, $keylen);
    # Apply document protection and Title, Subject, etc.

Controlling Dialogs and Printing Process

If you pass a Wx::PdfPrintData instance to the contructor of a Wx::PdfPrinter, Wx::PdfPrintDialog or Wx::PdfPrintPreview object you can control the printing process and dialog options that will be available to the user

    $flags = $pdata->GetPrintDialogFlags();

$flags is a or'd combination of

    Allow setting of the output filepath in dialog
    Allow setting of document properties (Title, Author, Subject, Keywords) in dialog

    Allow setting of protection options in dialog

    Show the "Launch document in default viewer" checkbox

    Allow no settings in dialog

    Allow all settings in dialog

Set the default value for the "Launch document in default viewer" checkbox

    $bool = $pdata->GetLaunchDocumentViewer();

Although not particularly useful when outputting a PDF document, you can set pages to print in the Wx::PdfPrintData in the same manner as Wx::PrintDialogData

    $pagenum = $pdata->GetFromPage();
    $pagenum = $pdata->GetToPage();
    $pagenum = $pdata->GetMinPage();
    $pagenum = $pdata->GetMaxPage();

By default, the Wx::PdfPrinter sets up a Wx::PdfDC with a resolution of 600 pixels. perl inch. It does not make any difference to the output quality what you set the resolution to. The PDF format is not pixel based so it is simply a matter of coordinate conversion. However, the Wx::DC drawing functions are integer based and so always introduce rounding and truncation when coordinates are transformed. Therefore, the lower the resolution, the bigger the error you may experience. For example, truncating 600.49 to 600 on a virtual 600 dpi device is a much smaller error that truncating 72.49 to 72 on a virtual 72 dpi device. So you probably don't want to change the resolution defaults.

    $dpi = $pdata->GetPrintResolution();

Setting Template Mode

By default a Wx::PdfDC instance creates a Wx::PdfDocument and outputs the result. The Wx::PdfDC can also operate in 'template mode' where an existing Wx::PdfDocument instance is passed to the constructor. This can be useful to combine the text methods of Wx::PdfDocument with the graphics methods of Wx::PdfDC. In this mode, the StartDoc and EndDoc methods of the Wx::PdfDC have no effect. The Wx::PdfDocument is created outside the Wx::PdfDC instance and code must handle writing it to disk independently too.

To allow template mode to be used within the wxWidgets printing framework (perhaps you have a custom Wx::Printout instance designed to handle enhanced PDF features) you can set the template parameters in a Wx::PdfPrintData instance that gets passed to Wx::PdfPrinter or Wx::PdfPrintPreview.

    $pdata->SetTemplate( $wxpdfdocument, $width, $height );
    $pdfdoc =  $pdata->GetTemplateDocument();
    $width  =  $pdata->GetTemplateWidth();
    $height =  $pdata->GetTemplateHeight();
    # check if SetTemplate has been called
    $bool   =  $pdata->GetTemplateMode();


A drop in for Wx::PrintPreview used in exactly the same way accepting a Wx::PdfPrintData instance in its constructor.

    my $preview = Wx::PdfPrintPreview->new(
    my $frame = Wx::PreviewFrame->new( $printpreview, $self,
            "PDF Printing Preview", [-1, -1], [600, 600] );
    $frame->Show( 1 );

There are complete examples in the Wx::Demo demomodule provided with the distribution.


A drop in for Wx::Printer used in exactly the same way accepting a Wx::PdfPrintData instance in its constructor.

    my $printer = Wx::PdfPrinter->new($wxPdfPrintData);
    $printer->Print($parent, $wxPrintout, 1);

There are complete examples in the Wx::Demo demomodule provided with the distribution.

makefont and showfont

An interface is provided for calling the makefont and showfont utilities provided with wxPdfDocument. This is necessary to add the wxWidgets and wxPdfDocument libraries in your Perl installation to the appropriate LD_LIBRARY_PATH, DYLD_LIBRARY_PATH and PATH for your operating system and to set the font file location environment variables.

    my ($status, $stdout, $stderr) = Wx::PdfDocument::MakeFont( $makefontparams );
    my ($status, $stdout, $stderr) = Wx::PdfDocument::ShowFont( $showfontparams );




Launching a PDF Document Viewer

Wx::PdfDocument provides a static method that will query the system for the correct command to view a pdf document:


See Also


This 0.10 release builds wxPdfDocument 0.9.3 code with minor changes to Windows makefiles to allow building in the Perl / wxPerl environment. All applied patches are in the patches subfolder of the distribution.


Copyright (C) 2006 & 2012 by Mark Wardell

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.12 or, at your option, any later version of Perl 5 you may have available.

The wxPdfDocument C++ class was created by Ulrich Telle <>