C++ web框架 Paozhu 1.14.0 发布,html 生成 pdf 文件,xlsx 文件读写

发布于

### C++ web框架 Paozhu 1.14.0 发布

C++ web框架 Paozhu 1.14.0 发布,加入了 HTML 生成 PDF 文件的功能,目前全球几乎是唯一的 C++ 语言开源项目,速度非常快,自带图像处理功能,二维码模块为内置,不需要第三方库,因为 Paozhu 已经有了自己原生的图像处理类,目前支持 jpg 和 png 格式的图片,足以满足常规 web 开发的需求。

Paozhu 的子项目 webpdf 是一个 html、svg、images 生成 PDF 的功能,灵感来自 fpdf.php,重构为 C++ 代码,并且支持 ttf 和 otf 字体,同时进行了程序优化。

### 增加 Excel xlsx 读写功能

增加了 Excel xlst 的读写程序,可以实现 Excel 的读写功能,目前测试支持 Office 2006、macOS Number 和 LibreOffice 生成的 xlsx 格式。

### HTML 转 PDF 文件例子

```cpp
#include "httppeer.h"
#include "serverconfig.h"
#include "server_localvar.h"
#include "test_webpdf.h"
#include "func.h"
#include <memory>
#include <string>
#ifdef ENABLE_PDF
#include "webpdf.h"
#endif// ENABLE_PDF
namespace http
{
namespace fs = std::filesystem;
//@urlpath(null,test_webpdf)
std::string test_webpdf(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->get_peer();
#ifdef ENABLE_PDF
server_loaclvar &static_server_var = get_server_global_var();

if (static_server_var.config_path.size() < 5)
{
client << "<p> static_server_var.config_path empty </p>";
return "";
}

std::string file_conf = dir_name(static_server_var.config_path);

if (file_conf.size() > 0 && file_conf.back() != '/')
{
file_conf.push_back('/');
}
file_conf.append("docs/");

if(peer->pathinfos.size() == 2 && peer->pathinfos[1] == "cn")
{
try
{
std::string html_file = file_conf + "webpdf_tutorial.html";
std::string pdf_file = file_conf + "webpdf_tutorial.pdf";
std::string font_file = "AlibabaPuHuiTi-Light.otf";
std::string font_name = "AlibabaPuHuiTi-Light";

fs::path html_path = fs::absolute(html_file);
fs::path pdf_path = fs::absolute(pdf_file);
fs::path font_dir = fs::absolute(file_conf + "font");

std::ifstream f(html_file, std::ios::binary);
if (!f.is_open())
{
client << "Cannot open file: " << html_file;
return "";
}

// 获取文件大小
f.seekg(0, std::ios::end);
std::streamsize size = f.tellg();
f.seekg(0, std::ios::beg);

std::string html;
html.resize(static_cast<size_t>(size));
if (size > 0 && !f.read(html.data(), size)) {
client << "Failed to read file: " << html_file;
return "";
}

auto pdf = std::make_unique<pz::webpdf>();
pdf->setImagesPath(file_conf);
pdf->AddPage();
pdf->AddFont(font_name,"",font_file,font_dir.string());
pdf->SetFont(font_name,"",14);
fs::current_path(fs::absolute(html_file).parent_path());
pdf->WriteHTML(html);

peer->type("application/pdf");
pdf->Output("F", pdf_path.string());
peer->output = pdf->Output("D", pdf_path.string());
//peer->output = pdf->move_result();

} catch (const std::exception& e) {
client << "Error: " << e.what();
return "";
}
}
else
{
try
{
std::string html_file = file_conf + "webpdf_tutorial_en.html";
std::string pdf_file = file_conf + "webpdf_tutorial_en.pdf";
std::string font_file = "AlibabaSans-Regular.otf";
std::string font_name = "AlibabaSans-Regular";

fs::path html_path = fs::absolute(html_file);
fs::path pdf_path = fs::absolute(pdf_file);
fs::path font_dir = fs::absolute(file_conf + "font");

std::ifstream f(html_file, std::ios::binary);
if (!f.is_open())
{
client << "Cannot open file: " << html_file;
return "";
}

// 获取文件大小
f.seekg(0, std::ios::end);
std::streamsize size = f.tellg();
f.seekg(0, std::ios::beg);

std::string html;
html.resize(static_cast<size_t>(size));
if (size > 0 && !f.read(html.data(), size)) {
client << "Failed to read file: " << html_file;
return "";
}

auto pdf = std::make_unique<pz::webpdf>();
pdf->setImagesPath(file_conf);
pdf->AddFont(font_name, "", font_file, font_dir.string());
pdf->SetFont(font_name, "", 14);

pdf->WriteHTML(html);
peer->type("application/pdf");
pdf->Output("F", pdf_path.string());
peer->output = pdf->Output("D", pdf_path.string());
//peer->output = pdf->move_result();

} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return "";
}
}
#else
client << "<p>Please: cmake .. -DENABLE_PDF=ON </p>";
#endif// ENABLE_PDF

return "";
}

//@urlpath(null,test_ttfpdf)
std::string test_otfpdf(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->get_peer();
#ifdef ENABLE_PDF
// ...
#endif// ENABLE_PDF
}
```

### Excel xlsx 读写程序例子

```cpp
#include "httppeer.h"
#include "serverconfig.h"
#include "server_localvar.h"
#include "test_pzexcel.h"
#include "func.h"
#include <memory>
#include <string>
#ifdef ENABLE_EXCEL
#include "pzexcel.h"
#endif// ENABLE_EXCEL
namespace http
{
//@urlpath(null,test_pzexcel)
std::string test_pzexcel(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->get_peer();
#ifdef ENABLE_EXCEL
// ...
#endif// ENABLE_EXCEL
}
}
```

### 二维码生成例子

```cpp
#include <chrono>
#include <thread>
#include "httppeer.h"
#include <filesystem>
#ifdef ENABLE_IMAGE
#include "qrcode.h"
#include "pzpng.h"
#endif

#include "testqrcode.h"
namespace http
{
//@urlpath(null,testqrcode)
std::string testqrcode(std::shared_ptr<httppeer> peer)
{
httppeer &client = peer->get_peer();
std::string atxt = client.post["qrcontent"].to_string();
if (atxt.size() == 0)
{
atxt = "qrsting hello world !";
}
client << "<p>" << atxt << "</p>";

#ifdef ENABLE_IMAGE
// ...
#endif
}
}
```

---

原文链接:[点击查看](https://www.oschina.net/news/476009)

评论

暂无评论。