Paozhu logoPaozhu Docs
教程目录
DEV

高级模块

PDF、Office、ZIP、邮件、短信、支付与图表模块的编译开关和基本用法。

编译开关

terminalbash
cmake .. -DCMAKE_BUILD_TYPE=Release \
  -DENABLE_PDF=ON -DENABLE_OFFICE=ON -DENABLE_ZIP=ON \
  -DENABLE_SMS=ON -DENABLE_WEBPAY=ON -DENABLE_CHART=ON
make -j8

这些模块默认全部关闭。ENABLE_OFFICE 会自动拉起 ENABLE_ZIP。

PDF

controller/src/test_webpdf.cppcpp
#include "webpdf/webpdf.h"
pz::webpdf pdf;
pdf.AddPage();
pdf.AddFont("msyh.ttc", "msyh");
pdf.SetFont("msyh", 12);
pdf.WriteHTML("<h1>Paozhu PDF</h1><p>Hello from C++.</p>");
pdf.Output("F", "./temp/report.pdf");

Office / ZIP

pz::excel、pz::word 与 pz::zip 提供读写、单元格操作与压缩包处理。setCellColor 当前表示单元格背景色。

邮件与短信

controller/src/test_email.cppcpp
email::send_email mail;
mail.smpturl = "smtp.example.com";
mail.isssl = true;
mail.port = 465;
mail.username = "no-reply@example.com";
mail.password = "secret";
mail.send();

短信通过 sms::send_sms("aliyun").send() 调用;邮件字段 smpturl 的拼写就是源码中的写法。

支付、微信与图表

支付宝走 pay::alipay;微信支付 V2 用 MD5/XML,V3 用 SHA256-RSA2048/JSON。图表模块 pzcharts 提供 17 种 Svg*Chart,统一 render() 输出 SVG。密钥与凭据不要硬编码。

二维码生成

Paozhu 内置了基于 qrencode 和自带 PNG 编码器的二维码生成模块。需要同时开启两个编译选项:ENABLE_GD 拉起 GD 图形栈和 qrencode 库,ENABLE_IMAGE 提供 PNG 图像类把二维码矩阵渲染成文件。

编译开关与系统依赖

终端bash
# macOS
brew install qrencode

# Debian / Ubuntu
sudo apt install libqrencode-dev

# Windows (vcpkg)
vcpkg install qrencode

# 构建时两个开关一起开
cmake .. -DCMAKE_BUILD_TYPE=Release \
  -DENABLE_GD=ON -DENABLE_IMAGE=ON
make -j8

基本用法

构造 qr::qrcode,调用 .text(content, ecc, version) 编码字符串,再把原始矩阵(q.data / q.width() / q.height())交给 image::png::qrdata() 绘制,最后保存为 PNG。

controller/src/testqrcode.cppcpp
#ifdef ENABLE_IMAGE
#include "qrcode.h"
#include "pzpng.h"
#endif

//@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.empty()) atxt = "hello paozhu";

#ifdef ENABLE_IMAGE
    qr::qrcode q;
    q.text(atxt, qr::Ecc::M, 1);       // 内容、纠错级别、版本提示

    image::png img;
    unsigned char scale = 10;
    unsigned int border = 20;
    img.create(q.width()*scale + border*2,
               q.height()*scale + border*2, 6, 8);
    img.fillColor({255, 255, 255, 255});
    img.qrdata(q.data, q.width(), q.height(), scale, border, border);

    std::string upload = client.get_sitepath();
    upload += "/upload/test.png";
    img.save(upload);

    client << "<img src=\"/upload/test.png\">";
#endif
    return "";
}

API 参考

API说明
qr::qrcode q;构造空 QR 编码器(头文件 qrcode.h)
q.text(content, ecc, version)编码字符串;ecc 为 qr::Ecc::L/M/Q/H,version = 1–40(0 自动选择)
q.data黑白矩阵原始数据(vector<unsigned char>)
q.width()、q.height()编码后的 QR 矩阵宽高
q.version实际使用的 QR 版本(传 0 时自动选择)
image::png img;自带 PNG 写入器(头文件 pzpng.h)
img.create(w, h, color_type, bit_depth)创建空白画布
img.fillColor({r,g,b,a})背景填充(白色 {255,255,255,255})
img.qrdata(q.data, q.w(), q.h(), scale, ox, oy)把 QR 矩阵绘制到画布
img.save(path)写入 PNG 到磁盘