libharu生成pdf
1 概述
使用libharu生成pdf。
2 主要功能点
此演示工程展示以下功能:
1)生成饼状图;
2)中文字体;
3)png图片。
4)其他示例可参考libharu源码https://gitcode.com/gh_mirrors/li/libharu/tree/master/demo
3 源码下载与导入
1)下载地址:https://gitee.com/clawclaw/sttharu/repository/archive/master.zip
或git clone https://gitee.com/clawclaw/sttharu.git
2)下载后解压,再导入到ApusIDE工作空间打开,具体操作请参考“项目导入”。
3)同步一下项目头文件
4 第三方库配置
4.1源码中已经包含了第三方库libhpdf, libz, libpng
4.2模拟器编译配置
a)头文件路径
${workspace_loc:/${ProjName}/src/hura}
b)库名及路径
libhpdf
${workspace_loc:/${ProjName}/Debug}
4.3 屏的编译设置
a)头文件
${workspace_loc:/${ProjName}/src/hura}
b)库名及路径
hpdf
${workspace_loc:/${ProjName}/Release}
5 部分主要源码
#include "Frm02.h"
#include "util/StringUtil.h"
#include "hpdf.h"
Frm02::Frm02(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
hmiApp = app;
}
Frm02::~Frm02() {
}
void Frm02::onInit() {
}
void Frm02::createParts() {
setMainArg(0, 0, 1024, 600);
setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);
/*----------------------------- Button : wMButton1 -----------------------------*/
wMButton1 = new STTButton(p_rt, this, "wMButton1", 0);
wMButton1->setMainArg(1, 1, 198, 48, false);
wMButton1->setPressedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
wMButton1->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
wMButton1->setReleasedArg("返回主页", 0x0, "文泉驿微米黑", 16, 0);
wMButton1->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
wMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);
/*----------------------------- Button : wMButton2 -----------------------------*/
wMButton2 = new STTButton(p_rt, this, "wMButton2", 0);
wMButton2->setMainArg(38, 79, 98, 48, false);
wMButton2->setPressedArg("饼状图", 0x0, "文泉驿微米黑", 16, 0);
wMButton2->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
wMButton2->setReleasedArg("饼状图", 0x0, "文泉驿微米黑", 16, 0);
wMButton2->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
wMButton2->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton2_clk_cb);
/*----------------------------- Button : wMButton3 -----------------------------*/
wMButton3 = new STTButton(p_rt, this, "wMButton3", 0);
wMButton3->setMainArg(137, 79, 98, 48, false);
wMButton3->setPressedArg("中文字", 0x0, "文泉驿微米黑", 16, 0);
wMButton3->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
wMButton3->setReleasedArg("中文字", 0x0, "文泉驿微米黑", 16, 0);
wMButton3->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
wMButton3->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton3_clk_cb);
/*----------------------------- Button : wMButton4 -----------------------------*/
wMButton4 = new STTButton(p_rt, this, "wMButton4", 0);
wMButton4->setMainArg(236, 79, 98, 48, false);
wMButton4->setPressedArg("png图片", 0x0, "文泉驿微米黑", 16, 0);
wMButton4->setPressedStyle(8, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
wMButton4->setReleasedArg("png图片", 0x0, "文泉驿微米黑", 16, 0);
wMButton4->setReleasedStyle(5, 255, 0xebebeb, 0x999999, 1, 1, 0x999999, 1, 0xebebeb);
wMButton4->onEventHandler(Event::CLICKED, this, (EHandler) &Frm02::wMButton4_clk_cb);
/*called after widget created.*/
return; //createParts END
}
void Frm02::onLoad() {
}
bool Frm02::onClosing() {
return true;
}
void Frm02::onDispose() {
}
TTable* generateData() {
TTable* data = new TTable();
data->addColumn("id");
data->addColumn("name");
data->addColumn("age");
data->addColumn("birthday");
data->addColumn("hobby");
for(int i=0;i<100;i++) {
string s = StringUtil::snprintf("id%d,姓名%d,年龄%d,生日%d,爱好%d",i,i,i,i,i);
data->addRow(s);
}
return data;
}
int demo1() {
HPDF_Doc pdf;
HPDF_Page page;
char fname[256];
HPDF_Point pos;
#if __arm__
strcpy (fname, "/customer/prm/data/demo1.pdf");
#else
strcpy (fname, "./data/demo1.pdf");
#endif
pdf = HPDF_New (NULL, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
// if (setjmp(env)) {
// HPDF_Free (pdf);
// return 1;
// }
/* add a new page object. */
page = HPDF_AddPage (pdf);
HPDF_Page_SetHeight (page, 220);
HPDF_Page_SetWidth (page, 200);
/* draw grid to the page */
// print_grid (pdf, page);
/* draw pie chart
*
* A: 45% Red
* B: 25% Blue
* C: 15% green
* D: other yellow
*/
/* A */
HPDF_Page_SetRGBFill (page, 1.0, 0, 0);
HPDF_Page_MoveTo (page, 100, 100);
HPDF_Page_LineTo (page, 100, 180);
HPDF_Page_Arc (page, 100, 100, 80, 0, 360 * 0.45);
pos = HPDF_Page_GetCurrentPos (page);
HPDF_Page_LineTo (page, 100, 100);
HPDF_Page_Fill (page);
/* B */
HPDF_Page_SetRGBFill (page, 0, 0, 1.0);
HPDF_Page_MoveTo (page, 100, 100);
HPDF_Page_LineTo (page, pos.x, pos.y);
HPDF_Page_Arc (page, 100, 100, 80, 360 * 0.45, 360 * 0.7);
pos = HPDF_Page_GetCurrentPos (page);
HPDF_Page_LineTo (page, 100, 100);
HPDF_Page_Fill (page);
/* C */
HPDF_Page_SetRGBFill (page, 0, 1.0, 0);
HPDF_Page_MoveTo (page, 100, 100);
HPDF_Page_LineTo (page, pos.x, pos.y);
HPDF_Page_Arc (page, 100, 100, 80, 360 * 0.7, 360 * 0.85);
pos = HPDF_Page_GetCurrentPos (page);
HPDF_Page_LineTo (page, 100, 100);
HPDF_Page_Fill (page);
/* D */
HPDF_Page_SetRGBFill (page, 1.0, 1.0, 0);
HPDF_Page_MoveTo (page, 100, 100);
HPDF_Page_LineTo (page, pos.x, pos.y);
HPDF_Page_Arc (page, 100, 100, 80, 360 * 0.85, 360);
pos = HPDF_Page_GetCurrentPos (page);
HPDF_Page_LineTo (page, 100, 100);
HPDF_Page_Fill (page);
/* draw center circle */
HPDF_Page_SetGrayStroke (page, 0);
HPDF_Page_SetGrayFill (page, 1);
HPDF_Page_Circle (page, 100, 100, 30);
HPDF_Page_Fill (page);
/* save the document to a file */
HPDF_SaveToFile (pdf, fname);
/* clean up */
HPDF_Free (pdf);
return 0;
}
int demo2() {
const char* SAMP_TXT = "武汉鑫通态科技有限公司/SIMTUTAI.";
HPDF_Doc pdf;
char fname[256];
HPDF_Page page;
HPDF_Font title_font;
HPDF_Font detail_font;
const char *detail_font_name;
HPDF_BOOL embed = HPDF_TRUE;
HPDF_REAL page_height;
HPDF_REAL page_width;
HPDF_REAL pw;
#if __arm__
strcpy (fname, "/customer/prm/data/demo2.pdf");
#else
strcpy (fname, "./data/demo2.pdf");
#endif
pdf = HPDF_New (NULL, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
/* Add a new page object. */
page = HPDF_AddPage (pdf);
HPDF_UseUTFEncodings(pdf);
HPDF_SetCurrentEncoder(pdf, "UTF-8");
detail_font_name = HPDF_LoadTTFontFromFile (pdf, "./fnt/wenquanyiweimihei.ttf", embed);
detail_font = HPDF_GetFont (pdf, detail_font_name, "UTF-8");
HPDF_Page_SetFontAndSize (page, detail_font, 10);
HPDF_Page_BeginText (page);
/* Move the position of the text to top of the page. */
HPDF_Page_MoveTextPos(page, 10, 190);
HPDF_Page_ShowText (page, detail_font_name);
if (embed)
HPDF_Page_ShowText (page, "(Embedded Subset)");
HPDF_Page_SetFontAndSize (page, detail_font, 15);
HPDF_Page_MoveTextPos (page, 10, -20);
HPDF_Page_ShowText (page, "人之初");
HPDF_Page_MoveTextPos (page, 0, -20);
HPDF_Page_ShowText (page, "性本善");
HPDF_Page_MoveTextPos (page, 0, -20);
HPDF_Page_ShowText (page, "1234567890");
HPDF_Page_MoveTextPos (page, 0, -20);
HPDF_Page_SetFontAndSize (page, detail_font, 10);
HPDF_Page_ShowText (page, SAMP_TXT);
HPDF_Page_MoveTextPos (page, 0, -18);
HPDF_Page_SetFontAndSize (page, detail_font, 16);
HPDF_Page_ShowText (page, SAMP_TXT);
HPDF_Page_MoveTextPos (page, 0, -27);
HPDF_Page_SetFontAndSize (page, detail_font, 23);
HPDF_Page_ShowText (page, SAMP_TXT);
HPDF_Page_MoveTextPos (page, 0, -36);
HPDF_Page_SetFontAndSize (page, detail_font, 30);
HPDF_Page_ShowText (page, SAMP_TXT);
HPDF_Page_MoveTextPos (page, 0, -36);
pw = HPDF_Page_TextWidth (page, SAMP_TXT);
page_height = 210;
page_width = pw + 40;
HPDF_Page_SetWidth (page, page_width);
HPDF_Page_SetHeight (page, page_height);
/* Finish to print text. */
HPDF_Page_EndText (page);
HPDF_Page_SetLineWidth (page, 0.5);
HPDF_Page_MoveTo (page, 10, page_height - 25);
HPDF_Page_LineTo (page, page_width - 10, page_height - 25);
HPDF_Page_Stroke (page);
HPDF_Page_MoveTo (page, 10, page_height - 85);
HPDF_Page_LineTo (page, page_width - 10, page_height - 85);
HPDF_Page_Stroke (page);
HPDF_SaveToFile (pdf, fname);
/* clean up */
HPDF_Free (pdf);
return 0;
}
void
draw_image (HPDF_Doc pdf,
const char *filename,
float x,
float y,
const char *text)
{
const char* FILE_SEPARATOR = "/";
char filename1[255];
HPDF_Page page = HPDF_GetCurrentPage (pdf);
HPDF_Image image;
image = HPDF_LoadPngImageFromFile (pdf, filename);
/* Draw image to the canvas. */
HPDF_Page_DrawImage (page, image, x, y, HPDF_Image_GetWidth (image),
HPDF_Image_GetHeight (image));
/* Print the text. */
// HPDF_Page_BeginText (page);
// HPDF_Page_SetTextLeading (page, 16);
// HPDF_Page_MoveTextPos (page, x, y);
// HPDF_Page_ShowTextNextLine (page, filename);
// HPDF_Page_ShowTextNextLine (page, text);
// HPDF_Page_EndText (page);
}
int demo3() {
HPDF_Doc pdf;
// HPDF_Font font;
HPDF_Page page;
char fname[256];
HPDF_Destination dst;
#if __arm__
strcpy (fname, "/customer/prm/data/demo3.pdf");
#else
strcpy (fname, "./data/demo3.pdf");
#endif
pdf = HPDF_New (NULL, NULL);
if (!pdf) {
printf ("error: cannot create PdfDoc object\n");
return 1;
}
// HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
/* add a new page object. */
page = HPDF_AddPage (pdf);
HPDF_Page_SetWidth (page, 550);
HPDF_Page_SetHeight (page, 650);
dst = HPDF_Page_CreateDestination (page);
HPDF_Destination_SetXYZ (dst, 0, HPDF_Page_GetHeight (page), 1);
HPDF_SetOpenAction(pdf, dst);
HPDF_Page_BeginText (page);
// HPDF_Page_SetFontAndSize (page, font, 20);
HPDF_Page_MoveTextPos (page, 220, HPDF_Page_GetHeight (page) - 70);
// HPDF_Page_ShowText (page, "PngDemo");
HPDF_Page_EndText (page);
// HPDF_Page_SetFontAndSize (page, font, 12);
#if __arm__
draw_image (pdf, "/customer/prm/res/pic/add.png", 100, HPDF_Page_GetHeight (page) - 150,
"png.");
#else
draw_image (pdf, "./res/pic/add.png", 100, HPDF_Page_GetHeight (page) - 150,
"png.");
#endif
/* save the document to a file */
HPDF_SaveToFile (pdf, fname);
/* clean up */
HPDF_Free (pdf);
return 0;
}
void Frm02::wMButton2_clk_cb(uint16_t code, LvEvent e) {
/*wMButton2(功能键)的点击事件*/
demo1();
}
void Frm02::wMButton3_clk_cb(uint16_t code, LvEvent e) {
/*wMButton2(功能键)的点击事件*/
demo2();
}
void Frm02::wMButton4_clk_cb(uint16_t code, LvEvent e) {
/*wMButton2(功能键)的点击事件*/
demo3();
}
6 操作说明
点击按钮分别在data下生成demo1.pdf(饼状图), demo2.pdf(中文字),demo3.pdf(png图片)