c++wtlibharu

Rendering PDF's with Wt and libHARU Undefined Reference


I am trying to generate PDF's using the libHARU library in conjunction with the Wt Web Toolkit. I am trying to build example code that they provide in their Wt Widget Gallery (Wt PDF Renderer) that exports a PDF from XHTML.

I am compiling using g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 without C++11 features enabled.

I have provided the sample code below that is a direct copy from what is on their site.

The error that I am getting is as follows:

In function `ReportResource::renderPdf(Wt::WString const&, _HPDF_Doc_Rec*)':
undefined reference to `Wt::Render::WPdfRenderer::WPdfRenderer(_HPDF_Doc_Rec*, _HPDF_Dict_Rec*)'
undefined reference to `Wt::Render::WPdfRenderer::setMargin(double, Wt::WFlags<Wt::Side>)'
undefined reference to `Wt::Render::WPdfRenderer::setDpi(int)'
undefined reference to `Wt::Render::WPdfRenderer::~WPdfRenderer()'
undefined reference to `Wt::Render::WPdfRenderer::~WPdfRenderer()'  

I do not understand the reason for this error as I have built and linked the libHARU library into my project. Just as a sanity check - I can right click on the Wt::Render::WPdfRenderer::WPdfRenderer and open the declaration (if you are familiar with the Eclipse IDE) and Eclipse takes me right to the header file for this class. What is causing the "undefined reference errors"?

namespace {
    void error_handler(HPDF_STATUS error_no, HPDF_STATUS detail_no, void *user_data) {
    fprintf(stderr, "libharu error: error_no=%04X, detail_no=%d\n", 
              (unsigned int) error_no, (int) detail_no);
    }
}

class ReportResource : public Wt::WResource
{

    public:
        ReportResource(Wt::WObject *parent = 0)
        : Wt::WResource(parent)
        {
            suggestFileName("cycle-times.pdf");
        }

        virtual void handleRequest(const Wt::Http::Request& request,
                                   Wt::Http::Response& response)
        {
            response.setMimeType("application/pdf");

        HPDF_Doc pdf = HPDF_New(error_handler, 0);

        // Note: UTF-8 encoding (for TrueType fonts) is only available since libharu 2.3.0 !
        HPDF_UseUTFEncodings(pdf);

        renderReport(pdf);

        HPDF_SaveToStream(pdf);
        unsigned int size = HPDF_GetStreamSize(pdf);
        HPDF_BYTE *buf = new HPDF_BYTE[size];
        HPDF_ReadFromStream (pdf, buf, &size);
        HPDF_Free(pdf);
        response.out().write((char*)buf, size);
        delete[] buf;
        }

    private:
        void renderReport(HPDF_Doc pdf) {
            renderPdf(Wt::WString::tr("report.example"), pdf);
        }

        void renderPdf(const Wt::WString& html, HPDF_Doc pdf)
        {
        HPDF_Page page = HPDF_AddPage(pdf);
        HPDF_Page_SetSize(page, HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT);

        Wt::Render::WPdfRenderer renderer(pdf, page);

        renderer.setMargin(2.54);
        renderer.setDpi(96);
        renderer.render(html);
        }
    };

Solution

  • The PDF is generated using The Haru Free PDF Library, and this class is included in the library only if libharu was found during the build of the library.

    Anyone finding this question needs to build libharu first and then tell cmake about it before building Wt like so:

     cmake ../ -DHARU_PREFIX=/path/to/libharu
    

    If libharu is not built or cannot be found, you will see an error message in the output from cmake similar to this:

    libharu libraries not found - set HARU_PREFIX after running cmake ../