使用Wt和libHARU未定义参考渲染PDF

时间:2014-01-29 18:25:12

标签: c++ wt libharu

我正在尝试使用libHARU libraryWt Web Toolkit生成PDF。我正在尝试构建他们在Wt Widget Gallery(Wt PDF Renderer)中提供的示例代码,该代码从XHTML导出PDF。

我正在使用g ++编译(Ubuntu / Linaro 4.6.3-1ubuntu5)4.6.3 没有启用C ++ 11功能。

我在下面提供了示例代码,它是来自其网站上的内容的直接副本。

我得到的错误如下:

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()'  

我不明白这个错误的原因,因为我已经将libHARU库构建并链接到我的项目中。就像一个完整性检查 - 我可以右键单击Wt::Render::WPdfRenderer::WPdfRenderer并打开声明(如果您熟悉Eclipse IDE),Eclipse会将我带到此类的头文件。是什么导致了“未定义的参考错误”?

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);
        }
    };

1 个答案:

答案 0 :(得分:1)

PDF是使用Haru Free PDF Library生成的,只有在构建库期间找到libharu时,此类才包含在库中。

任何发现此问题的人都需要首先构建libharu,然后在构建Wt之前告诉cmake:

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

如果未构建libharu或无法找到libharu,您将在cmake的输出中看到类似于以下内容的错误消息:

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