我正在尝试将libHARU 库与Wt Web Toolkit结合使用来生成 PDF 。我正在尝试构建他们在 Wt Widget Gallery (Wt PDF Renderer )中提供的示例代码 ) 中提供的示例代码,该代码从 XHTML 导出 PDF。
我正在使用 g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3编译启用 C++11 功能的 g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 进行编译。
我提供了下面的示例代码,它是他们网站上的直接副本。
我得到的错误如下:
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);
}
};