|
@@ -0,0 +1,550 @@
|
|
|
|
|
+//! 竣工回传单 PDF(工单§5.5.1「PDF 与竣工记录同批生成」)。
|
|
|
|
|
+//!
|
|
|
|
|
+//! 背景(2026-11-18 实测):`completion/generate@v1` 过去只在 `ticket_material` 登记
|
|
|
|
|
+//! `竣工回传/{竣工单号}.pdf` 这一行,**本体从未写进对象存储** ⇒ 页面能列出文件、点下载
|
|
|
|
|
+//! 拿到 403/404。本模块补上本体:生成一页 A4 的竣工回传单,由
|
|
|
|
|
+//! [`bos_platform::clients::PlatformClient::oss_upload_object`] 按同一对象键直传。
|
|
|
|
|
+//!
|
|
|
|
|
+//! 两条边界:
|
|
|
|
|
+//! 1. **内容全部从竣工事务的既有事实取**(单号/工单号/回填头/操作人),⛔ 接受前端传字段
|
|
|
|
|
+//! ——与 `completion.rs` 的「竣工内容⛔ 由入参提供」同一条纪律。
|
|
|
|
|
+//! 2. **不引 PDF 库**:版面固定为一页、无图片,直接用 PDF 1.4 语法写对象。
|
|
|
|
|
+//!
|
|
|
|
|
+//! 中文落地方式(2026-11-18 二次修正):
|
|
|
|
|
+//! * 首选**嵌入子集中文字体**([`crate::completion_pdf_font`]),`Identity-H` + `CIDToGIDMap
|
|
|
|
|
+//! /Identity`,正文写 GID。原因:标准 CJK 字体 `STSong-Light` 要求阅读器自带 Adobe-GB1
|
|
|
|
|
+//! 替代映射,**macOS 的 PDFKit/Quick Look 不做这个映射**,整页中文按替代字体(拉丁)的
|
|
|
|
|
+//! 字形号渲染 ⇒ 全是乱码;同一份文件在 PDFKit 里取文本却是对的(2026-11-18 复现)。
|
|
|
|
|
+//! * 万一运行环境找不到可嵌入的字体(容器没装中文字体),回落标准 CJK 字体 `STSong-Light`
|
|
|
|
|
+//! + `UniGB-UCS2-H`(Acrobat/WPS/Chrome 可读,仅 PDFKit 不可读),保证**不因缺字体
|
|
|
|
|
+//! 而让竣工归档整体失败**。
|
|
|
|
|
+//! * ASCII 行始终走 Helvetica 的 `(…)` 字面量:`UniGB-UCS2-H` 会把 `(JC…)` 按双字节读成
|
|
|
|
|
+//! 汉字(实测 `JC` ⇒ `䩃`),嵌入字体也不该把数字排成全角。
|
|
|
|
|
+
|
|
|
|
|
+use crate::completion_pdf_font::FontFace;
|
|
|
|
|
+use std::collections::{BTreeMap, BTreeSet};
|
|
|
|
|
+use std::fmt::Write as _;
|
|
|
|
|
+use std::io::Write as _;
|
|
|
|
|
+
|
|
|
|
|
+/// 竣工回传单的取值(调用方在竣工事务里逐项取好,本模块只排版)。
|
|
|
|
|
+#[derive(Debug, Clone)]
|
|
|
|
|
+pub struct Facts<'a> {
|
|
|
|
|
+ /// 竣工单号(`JC-…`),同时是对象键的文件名。
|
|
|
|
|
+ pub completion_no: &'a str,
|
|
|
|
|
+ pub ticket_no: &'a str,
|
|
|
|
|
+ /// `open`/`service_open`/`updown`/`expiry_stop`/`service_stop`;排版时转中文。
|
|
|
|
|
+ pub ticket_type: &'a str,
|
|
|
|
|
+ /// `open`(开通)/`stop`(停机);排版时转中文。
|
|
|
|
|
+ pub action: &'a str,
|
|
|
|
|
+ /// `success`/`partial`;排版时转中文。
|
|
|
|
|
+ pub conclusion: &'a str,
|
|
|
|
|
+ pub customer_id: &'a str,
|
|
|
|
|
+ pub customer_name: Option<&'a str>,
|
|
|
|
|
+ /// 联通订单号(追溯四号之一),无则 `None`。
|
|
|
|
|
+ pub unicom_order_no: Option<&'a str>,
|
|
|
|
|
+ /// 实际开通日/停机完成日(`YYYY-MM-DD`)。
|
|
|
|
|
+ pub exec_date: Option<&'a str>,
|
|
|
|
|
+ /// 回传日(业务日 `YYYY-MM-DD`)。
|
|
|
|
|
+ pub pushed_at: &'a str,
|
|
|
|
|
+ pub operator: &'a str,
|
|
|
|
|
+ pub ip_rule_version: &'a str,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 生成一页 A4 的竣工回传单(PDF 1.4 字节流)。
|
|
|
|
|
+pub fn render(f: &Facts<'_>) -> Vec<u8> {
|
|
|
|
|
+ let face = FontFace::load();
|
|
|
|
|
+ let mut p = Painter::new(face.as_ref());
|
|
|
|
|
+ layout(&mut p, f);
|
|
|
|
|
+ let content = p.c;
|
|
|
|
|
+
|
|
|
|
|
+ let mut objects: Vec<Vec<u8>> = Vec::with_capacity(11);
|
|
|
|
|
+ // 1 catalog
|
|
|
|
|
+ objects.push(b"<< /Type /Catalog /Pages 2 0 R >>".to_vec());
|
|
|
|
|
+ // 2 pages
|
|
|
|
|
+ objects.push(b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_vec());
|
|
|
|
|
+ // 3 page
|
|
|
|
|
+ objects.push(
|
|
|
|
|
+ b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595 842] \
|
|
|
|
|
+ /Resources << /Font << /F1 5 0 R /F2 6 0 R >> >> /Contents 4 0 R >>"
|
|
|
|
|
+ .to_vec(),
|
|
|
|
|
+ );
|
|
|
|
|
+ // 4 contents
|
|
|
|
|
+ objects.push(stream_obj("", content.as_bytes()));
|
|
|
|
|
+
|
|
|
|
|
+ // 6 Helvetica(纯 ASCII 行,避免中文字体把数字排成全角)——先占位,下面按模式补 5/7/8…
|
|
|
|
|
+ let helvetica = b"<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>".to_vec();
|
|
|
|
|
+
|
|
|
|
|
+ match &face {
|
|
|
|
|
+ None => {
|
|
|
|
|
+ // 回落:标准 CJK 字体,不嵌入。
|
|
|
|
|
+ objects.push(
|
|
|
|
|
+ b"<< /Type /Font /Subtype /Type0 /BaseFont /STSong-Light \
|
|
|
|
|
+ /Encoding /UniGB-UCS2-H /DescendantFonts [7 0 R] >>"
|
|
|
|
|
+ .to_vec(),
|
|
|
|
|
+ );
|
|
|
|
|
+ objects.push(helvetica);
|
|
|
|
|
+ objects.push(
|
|
|
|
|
+ b"<< /Type /Font /Subtype /CIDFontType0 /BaseFont /STSong-Light \
|
|
|
|
|
+ /CIDSystemInfo << /Registry (Adobe) /Ordering (GB1) /Supplement 4 >> \
|
|
|
|
|
+ /FontDescriptor 8 0 R /DW 1000 /W [1 95 500] >>"
|
|
|
|
|
+ .to_vec(),
|
|
|
|
|
+ );
|
|
|
|
|
+ objects.push(
|
|
|
|
|
+ b"<< /Type /FontDescriptor /FontName /STSong-Light /Flags 6 \
|
|
|
|
|
+ /FontBBox [-25 -254 1000 880] /ItalicAngle 0 /Ascent 880 /Descent -254 \
|
|
|
|
|
+ /CapHeight 880 /StemV 93 >>"
|
|
|
|
|
+ .to_vec(),
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ Some(face) => {
|
|
|
|
|
+ let used: BTreeSet<u16> = p.used.keys().copied().collect();
|
|
|
|
|
+ let program = face.subset(&used);
|
|
|
|
|
+ let tag = "KLTBOS";
|
|
|
|
|
+ let base = format!("/{tag}+{}", pdf_name(&face.ps_name));
|
|
|
|
|
+
|
|
|
|
|
+ // 5 Type0(Identity-H,正文是 GID)
|
|
|
|
|
+ objects.push(
|
|
|
|
|
+ format!(
|
|
|
|
|
+ "<< /Type /Font /Subtype /Type0 /BaseFont {base} /Encoding /Identity-H \
|
|
|
|
|
+ /DescendantFonts [7 0 R] /ToUnicode 9 0 R >>"
|
|
|
|
|
+ )
|
|
|
|
|
+ .into_bytes(),
|
|
|
|
|
+ );
|
|
|
|
|
+ objects.push(helvetica);
|
|
|
|
|
+ // 7 CIDFontType2
|
|
|
|
|
+ objects.push(
|
|
|
|
|
+ format!(
|
|
|
|
|
+ "<< /Type /Font /Subtype /CIDFontType2 /BaseFont {base} \
|
|
|
|
|
+ /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> \
|
|
|
|
|
+ /FontDescriptor 8 0 R /DW 1000 /CIDToGIDMap /Identity >>"
|
|
|
|
|
+ )
|
|
|
|
|
+ .into_bytes(),
|
|
|
|
|
+ );
|
|
|
|
|
+ // 8 FontDescriptor(字面量里的 FontName 须与 BaseFont 同名)
|
|
|
|
|
+ objects.push(
|
|
|
|
|
+ format!(
|
|
|
|
|
+ "<< /Type /FontDescriptor /FontName {base} /Flags 4 \
|
|
|
|
|
+ /FontBBox [{} {} {} {}] /ItalicAngle 0 /Ascent {} /Descent {} \
|
|
|
|
|
+ /CapHeight {} /StemV {} /FontFile2 10 0 R >>",
|
|
|
|
|
+ face.bbox[0],
|
|
|
|
|
+ face.bbox[1],
|
|
|
|
|
+ face.bbox[2],
|
|
|
|
|
+ face.bbox[3],
|
|
|
|
|
+ face.ascent,
|
|
|
|
|
+ face.descent,
|
|
|
|
|
+ face.cap_height,
|
|
|
|
|
+ face.stem_v
|
|
|
|
|
+ )
|
|
|
|
|
+ .into_bytes(),
|
|
|
|
|
+ );
|
|
|
|
|
+ // 9 ToUnicode(GID → Unicode,复制文本时用)
|
|
|
|
|
+ let cmap = to_unicode(&p.used);
|
|
|
|
|
+ objects.push(stream_obj("", cmap.as_bytes()));
|
|
|
|
|
+ // 10 FontFile2(子集,FlateDecode)
|
|
|
|
|
+ let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::best());
|
|
|
|
|
+ let _ = z.write_all(&program);
|
|
|
|
|
+ let compressed = z.finish().unwrap_or_else(|_| program.clone());
|
|
|
|
|
+ if compressed.len() < program.len() {
|
|
|
|
|
+ objects.push(stream_obj(
|
|
|
|
|
+ &format!("/Filter /FlateDecode /Length1 {}", program.len()),
|
|
|
|
|
+ &compressed,
|
|
|
|
|
+ ));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ objects.push(stream_obj(
|
|
|
|
|
+ &format!("/Length1 {}", program.len()),
|
|
|
|
|
+ &program,
|
|
|
|
|
+ ));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ assemble(&objects)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 依次写对象、xref、trailer。
|
|
|
|
|
+fn assemble(objects: &[Vec<u8>]) -> Vec<u8> {
|
|
|
|
|
+ let mut out = Vec::with_capacity(8192);
|
|
|
|
|
+ out.extend_from_slice(b"%PDF-1.4\n%\xE2\xE3\xCF\xD3\n");
|
|
|
|
|
+ let mut offsets = Vec::with_capacity(objects.len());
|
|
|
|
|
+ for (i, body) in objects.iter().enumerate() {
|
|
|
|
|
+ offsets.push(out.len());
|
|
|
|
|
+ out.extend_from_slice(format!("{} 0 obj\n", i + 1).as_bytes());
|
|
|
|
|
+ out.extend_from_slice(body);
|
|
|
|
|
+ out.extend_from_slice(b"\nendobj\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ let xref_at = out.len();
|
|
|
|
|
+ let count = objects.len() + 1;
|
|
|
|
|
+ out.extend_from_slice(format!("xref\n0 {count}\n").as_bytes());
|
|
|
|
|
+ out.extend_from_slice(b"0000000000 65535 f \n");
|
|
|
|
|
+ for off in offsets {
|
|
|
|
|
+ out.extend_from_slice(format!("{off:010} 00000 n \n").as_bytes());
|
|
|
|
|
+ }
|
|
|
|
|
+ out.extend_from_slice(
|
|
|
|
|
+ format!(
|
|
|
|
|
+ "trailer\n<< /Size {count} /Root 1 0 R >>\nstartxref\n{xref_at}\n%%EOF\n"
|
|
|
|
|
+ )
|
|
|
|
|
+ .as_bytes(),
|
|
|
|
|
+ );
|
|
|
|
|
+ out
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 流对象(字典里除 `/Length` 外的额外项由 `extra` 给)。
|
|
|
|
|
+fn stream_obj(extra: &str, data: &[u8]) -> Vec<u8> {
|
|
|
|
|
+ let mut v = format!("<< /Length {}{extra} >>\nstream\n", data.len()).into_bytes();
|
|
|
|
|
+ v.extend_from_slice(data);
|
|
|
|
|
+ v.extend_from_slice(b"\nendstream");
|
|
|
|
|
+ v
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// PDF 名字对象里不允许出现 `+`/空格等,做最小净化(子集前缀已由调用方加)。
|
|
|
|
|
+fn pdf_name(s: &str) -> String {
|
|
|
|
|
+ s.chars()
|
|
|
|
|
+ .map(|c| {
|
|
|
|
|
+ if c.is_ascii_alphanumeric() || matches!(c, '-' | '_' | '.') {
|
|
|
|
|
+ c
|
|
|
|
|
+ } else {
|
|
|
|
|
+ '-'
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ .collect()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// `ToUnicode` CMap:把 PDF 里的 GID 映射回 Unicode(否则复制/搜索是乱码)。
|
|
|
|
|
+fn to_unicode(used: &BTreeMap<u16, char>) -> String {
|
|
|
|
|
+ let mut s = String::with_capacity(512 + used.len() * 24);
|
|
|
|
|
+ s.push_str(
|
|
|
|
|
+ "/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n\
|
|
|
|
|
+ /CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n\
|
|
|
|
|
+ /CMapName /Adobe-Identity-UCS def\n/CMapType 2 def\n\
|
|
|
|
|
+ 1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n",
|
|
|
|
|
+ );
|
|
|
|
|
+ let items: Vec<(u16, char)> = used.iter().map(|(g, c)| (*g, *c)).collect();
|
|
|
|
|
+ for chunk in items.chunks(100) {
|
|
|
|
|
+ let _ = writeln!(s, "{} beginbfchar", chunk.len());
|
|
|
|
|
+ for (gid, ch) in chunk {
|
|
|
|
|
+ let mut hex = String::with_capacity(8);
|
|
|
|
|
+ let mut buf = [0u16; 2];
|
|
|
|
|
+ for unit in ch.encode_utf16(&mut buf).iter().copied() {
|
|
|
|
|
+ let _ = write!(hex, "{unit:04X}");
|
|
|
|
|
+ }
|
|
|
|
|
+ let _ = writeln!(s, "<{gid:04X}> <{hex}>");
|
|
|
|
|
+ }
|
|
|
|
|
+ s.push_str("endbfchar\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ s.push_str(
|
|
|
|
|
+ "endcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n",
|
|
|
|
|
+ );
|
|
|
|
|
+ s
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn ticket_type_name(v: &str) -> &str {
|
|
|
|
|
+ match v {
|
|
|
|
|
+ "open" => "开户",
|
|
|
|
|
+ "service_open" => "服务开通",
|
|
|
|
|
+ "updown" => "升降配",
|
|
|
|
|
+ "expiry_stop" => "到期停机",
|
|
|
|
|
+ "service_stop" => "服务停用",
|
|
|
|
|
+ other => other,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn action_name(v: &str) -> &str {
|
|
|
|
|
+ match v {
|
|
|
|
|
+ "open" => "开通",
|
|
|
|
|
+ "stop" => "停机",
|
|
|
|
|
+ other => other,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn conclusion_name(v: &str) -> &str {
|
|
|
|
|
+ match v {
|
|
|
|
|
+ "success" => "成功",
|
|
|
|
|
+ "partial" => "部分成功",
|
|
|
|
|
+ "fail" => "失败",
|
|
|
|
|
+ other => other,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 文本落笔方式:嵌入字体(写 GID)或标准 CJK 字体(写 UTF-16BE)。
|
|
|
|
|
+struct Painter<'a> {
|
|
|
|
|
+ mode: Mode<'a>,
|
|
|
|
|
+ c: String,
|
|
|
|
|
+ /// GID → 该 GID 对应的字符(供 `ToUnicode`)。
|
|
|
|
|
+ used: BTreeMap<u16, char>,
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+enum Mode<'a> {
|
|
|
|
|
+ Standard,
|
|
|
|
|
+ Embedded(&'a FontFace),
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+impl<'a> Painter<'a> {
|
|
|
|
|
+ fn new(face: Option<&'a FontFace>) -> Self {
|
|
|
|
|
+ Self {
|
|
|
|
|
+ mode: match face {
|
|
|
|
|
+ Some(f) => Mode::Embedded(f),
|
|
|
|
|
+ None => Mode::Standard,
|
|
|
|
|
+ },
|
|
|
|
|
+ c: String::with_capacity(2048),
|
|
|
|
|
+ used: BTreeMap::new(),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// 一行文本:ASCII 走 Helvetica,其余走中文字体。
|
|
|
|
|
+ fn text(&mut self, size: f64, x: f64, y: f64, s: &str) {
|
|
|
|
|
+ if s.is_ascii() {
|
|
|
|
|
+ let _ = write!(
|
|
|
|
|
+ self.c,
|
|
|
|
|
+ "BT /F2 {size:.1} Tf 1 0 0 1 {x:.1} {y:.1} Tm ({}) Tj ET\n",
|
|
|
|
|
+ escape_literal(s)
|
|
|
|
|
+ );
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ let face = match &self.mode {
|
|
|
|
|
+ Mode::Embedded(f) => Some(*f),
|
|
|
|
|
+ Mode::Standard => None,
|
|
|
|
|
+ };
|
|
|
|
|
+ let hex = match face {
|
|
|
|
|
+ Some(face) => {
|
|
|
|
|
+ let mut hex = String::with_capacity(s.len() * 4);
|
|
|
|
|
+ for ch in s.chars() {
|
|
|
|
|
+ let gid = match face.glyph(ch) {
|
|
|
|
|
+ Some(g) => {
|
|
|
|
|
+ self.used.entry(g).or_insert(ch);
|
|
|
|
|
+ g
|
|
|
|
|
+ }
|
|
|
|
|
+ // 缺字形:退到 `?`,不进 ToUnicode(避免把 `?` 的码位映射成别的字)。
|
|
|
|
|
+ None => face.glyph('?').unwrap_or(0),
|
|
|
|
|
+ };
|
|
|
|
|
+ let _ = write!(hex, "{gid:04X}");
|
|
|
|
|
+ }
|
|
|
|
|
+ hex
|
|
|
|
|
+ }
|
|
|
|
|
+ None => utf16_hex(s),
|
|
|
|
|
+ };
|
|
|
|
|
+ // 中文固定宽度 1000:10pt 时每字 10pt,行宽足够,不做自动折行。
|
|
|
|
|
+ let _ = write!(
|
|
|
|
|
+ self.c,
|
|
|
|
|
+ "BT /F1 {size:.1} Tf 1 0 0 1 {x:.1} {y:.1} Tm <{hex}> Tj ET\n"
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fn hline(&mut self, x1: f64, y1: f64, x2: f64, y2: f64) {
|
|
|
|
|
+ let _ = write!(self.c, "0.6 w {x1:.1} {y1:.1} m {x2:.1} {y2:.1} l S\n");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// 段标题(横线+中文小标题)。
|
|
|
|
|
+ fn section(&mut self, title: &str, y: &mut f64) {
|
|
|
|
|
+ self.text(11.0, 60.0, *y, title);
|
|
|
|
|
+ self.hline(60.0, *y - 5.0, 535.0, *y - 5.0);
|
|
|
|
|
+ *y -= 24.0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /// 一列行:标签在左,值在 170pt 处。
|
|
|
|
|
+ fn row(&mut self, label: &str, value: &str, y: &mut f64) {
|
|
|
|
|
+ self.text(10.0, 64.0, *y, label);
|
|
|
|
|
+ self.text(10.0, 170.0, *y, value);
|
|
|
|
|
+ *y -= 22.0;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn layout(p: &mut Painter<'_>, f: &Facts<'_>) {
|
|
|
|
|
+ let left = 60.0_f64;
|
|
|
|
|
+ let right = 535.0_f64;
|
|
|
|
|
+
|
|
|
|
|
+ // ── 标题区 ────────────────────────────────────────────────────────────
|
|
|
|
|
+ p.text(20.0, left, 780.0, "竣工回传单");
|
|
|
|
|
+ p.text(
|
|
|
|
|
+ 10.0,
|
|
|
|
|
+ left,
|
|
|
|
|
+ 758.0,
|
|
|
|
|
+ &format!("Completion Certificate / {}", f.completion_no),
|
|
|
|
|
+ );
|
|
|
|
|
+ p.hline(left, 746.0, right, 746.0);
|
|
|
|
|
+
|
|
|
|
|
+ // ── 追溯段 ────────────────────────────────────────────────────────────
|
|
|
|
|
+ let mut y = 716.0;
|
|
|
|
|
+ p.section("追溯信息", &mut y);
|
|
|
|
|
+ p.row("竣工单号", f.completion_no, &mut y);
|
|
|
|
|
+ p.row("我方工单号", f.ticket_no, &mut y);
|
|
|
|
|
+ p.row(
|
|
|
|
|
+ "联通订单号",
|
|
|
|
|
+ f.unicom_order_no.unwrap_or("无"),
|
|
|
|
|
+ &mut y,
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // ── 客户段 ────────────────────────────────────────────────────────────
|
|
|
|
|
+ y -= 10.0;
|
|
|
|
|
+ p.section("客户信息", &mut y);
|
|
|
|
|
+ p.row("客户主体号", f.customer_id, &mut y);
|
|
|
|
|
+ p.row(
|
|
|
|
|
+ "客户名称",
|
|
|
|
|
+ f.customer_name.unwrap_or("—"),
|
|
|
|
|
+ &mut y,
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // ── 交付段 ────────────────────────────────────────────────────────────
|
|
|
|
|
+ y -= 10.0;
|
|
|
|
|
+ p.section("交付结果", &mut y);
|
|
|
|
|
+ p.row("工单类型", ticket_type_name(f.ticket_type), &mut y);
|
|
|
|
|
+ p.row("操作类型", action_name(f.action), &mut y);
|
|
|
|
|
+ p.row("执行结论", conclusion_name(f.conclusion), &mut y);
|
|
|
|
|
+ p.row(
|
|
|
|
|
+ if f.ticket_type == "expiry_stop" || f.ticket_type == "service_stop" {
|
|
|
|
|
+ "停机完成日期"
|
|
|
|
|
+ } else {
|
|
|
|
|
+ "实际开通日期"
|
|
|
|
|
+ },
|
|
|
|
|
+ f.exec_date.unwrap_or("—"),
|
|
|
|
|
+ &mut y,
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ // ── 回传段 ────────────────────────────────────────────────────────────
|
|
|
|
|
+ y -= 10.0;
|
|
|
|
|
+ p.section("回传登记", &mut y);
|
|
|
|
|
+ p.row("回传方式", "人工回传(运营侧出竣工)", &mut y);
|
|
|
|
|
+ p.row("回传日期", f.pushed_at, &mut y);
|
|
|
|
|
+ p.row("操作人", f.operator, &mut y);
|
|
|
|
|
+ p.row("IP 规则表版本", f.ip_rule_version, &mut y);
|
|
|
|
|
+
|
|
|
|
|
+ // ── 页脚 ──────────────────────────────────────────────────────────────
|
|
|
|
|
+ p.hline(left, 92.0, right, 92.0);
|
|
|
|
|
+ p.text(
|
|
|
|
|
+ 9.0,
|
|
|
|
|
+ left,
|
|
|
|
|
+ 78.0,
|
|
|
|
|
+ "本单由运营支撑系统按回填事实生成,随竣工记录一并归档;竣工单号与工单号是运营商对账与追溯的唯一依据。",
|
|
|
|
|
+ );
|
|
|
|
|
+ p.text(
|
|
|
|
|
+ 9.0,
|
|
|
|
|
+ left,
|
|
|
|
|
+ 62.0,
|
|
|
|
|
+ "受控回退只重开工单,不回滚服务开通状态、实际开通日与本回传记录。",
|
|
|
|
|
+ );
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// PDF 字面量串转义(`\`/`(`/`)`;非 ASCII 不会走到这里)。
|
|
|
|
|
+fn escape_literal(s: &str) -> String {
|
|
|
|
|
+ let mut out = String::with_capacity(s.len());
|
|
|
|
|
+ for ch in s.chars() {
|
|
|
|
|
+ match ch {
|
|
|
|
|
+ '\\' => out.push_str("\\\\"),
|
|
|
|
|
+ '(' => out.push_str("\\("),
|
|
|
|
|
+ ')' => out.push_str("\\)"),
|
|
|
|
|
+ '\r' | '\n' => out.push(' '),
|
|
|
|
|
+ _ => out.push(ch),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ out
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// 文本 → `UniGB-UCS2-H` 的 2 字节十六进制串(BMP 之外的字符降级为 `?`)。
|
|
|
|
|
+fn utf16_hex(s: &str) -> String {
|
|
|
|
|
+ let mut out = String::with_capacity(s.len() * 4);
|
|
|
|
|
+ for unit in s.encode_utf16() {
|
|
|
|
|
+ let _ = write!(out, "{unit:04X}");
|
|
|
|
|
+ }
|
|
|
|
|
+ out
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+#[cfg(test)]
|
|
|
|
|
+mod tests {
|
|
|
|
|
+ use super::*;
|
|
|
|
|
+
|
|
|
|
|
+ fn facts() -> Facts<'static> {
|
|
|
|
|
+ Facts {
|
|
|
|
|
+ completion_no: "JC-20261116-15",
|
|
|
|
|
+ ticket_no: "T-20261116-20",
|
|
|
|
|
+ ticket_type: "open",
|
|
|
|
|
+ action: "open",
|
|
|
|
|
+ conclusion: "success",
|
|
|
|
|
+ customer_id: "CUST-0001",
|
|
|
|
|
+ customer_name: Some("上海测试客户"),
|
|
|
|
|
+ unicom_order_no: Some("UNI-9"),
|
|
|
|
|
+ exec_date: Some("2026-11-16"),
|
|
|
|
|
+ pushed_at: "2026-11-18",
|
|
|
|
|
+ operator: "总运营",
|
|
|
|
|
+ ip_rule_version: "v1",
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn skeleton_is_a_valid_pdf() {
|
|
|
|
|
+ let pdf = render(&facts());
|
|
|
|
|
+ // ⚠️ 不能用 `from_utf8_lossy` 后再定位:头部含非 UTF-8 的二进制标记,
|
|
|
|
|
+ // 替换字符会让字符下标与字节下标错位(本测试第一版就栽在这儿)。
|
|
|
|
|
+ assert!(pdf.starts_with(b"%PDF-1.4"), "缺 PDF 头");
|
|
|
|
|
+ let hay = String::from_utf8_lossy(&pdf[..]);
|
|
|
|
|
+ assert!(hay.contains("/Type /Catalog"));
|
|
|
|
|
+ assert!(hay.contains("/Identity-H") || hay.contains("/UniGB-UCS2-H"));
|
|
|
|
|
+ assert!(pdf.windows(9).any(|w| w == b"startxref"));
|
|
|
|
|
+ assert!(pdf.ends_with(b"%%EOF\n") || pdf.ends_with(b"%%EOF"));
|
|
|
|
|
+ let sm = b"startxref\n";
|
|
|
|
|
+ let so = pdf.windows(sm.len()).position(|w| w == sm).expect("startxref");
|
|
|
|
|
+ let tail = &pdf[so + sm.len()..];
|
|
|
|
|
+ let end = tail.iter().position(|&b| b == b'\n').expect("偏移行");
|
|
|
|
|
+ let xref_at: usize = std::str::from_utf8(&tail[..end])
|
|
|
|
|
+ .unwrap()
|
|
|
|
|
+ .trim()
|
|
|
|
|
+ .parse()
|
|
|
|
|
+ .expect("startxref 偏移");
|
|
|
|
|
+ assert!(pdf[xref_at..].starts_with(b"xref"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn stream_length_matches_bytes() {
|
|
|
|
|
+ let pdf = render(&facts());
|
|
|
|
|
+ let marker = b"<< /Length ";
|
|
|
|
|
+ let i = pdf
|
|
|
|
|
+ .windows(marker.len())
|
|
|
|
|
+ .position(|w| w == marker)
|
|
|
|
|
+ .expect("contents 对象");
|
|
|
|
|
+ let rest = &pdf[i + marker.len()..];
|
|
|
|
|
+ let sp = rest.iter().position(|&b| b == b' ').expect("长度后的空格");
|
|
|
|
|
+ let n: usize = std::str::from_utf8(&rest[..sp]).unwrap().parse().unwrap();
|
|
|
|
|
+ let sm = b"stream\n";
|
|
|
|
|
+ let so = pdf[i..]
|
|
|
|
|
+ .windows(sm.len())
|
|
|
|
|
+ .position(|w| w == sm)
|
|
|
|
|
+ .expect("stream 起始");
|
|
|
|
|
+ let start = i + so + sm.len();
|
|
|
|
|
+ assert_eq!(pdf[start + n], b'\n', "endstream 前须有换行");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn ascii_still_uses_helvetica_literals() {
|
|
|
|
|
+ // ASCII 行始终走 Helvetica:既避开全角数字,也不受 CJK 字体是否可用影响。
|
|
|
|
|
+ let pdf = String::from_utf8_lossy(&render(&facts())).to_string();
|
|
|
|
|
+ assert!(pdf.contains("(JC-20261116-15) Tj"));
|
|
|
|
|
+ assert!(pdf.contains("(2026-11-18) Tj"));
|
|
|
|
|
+ assert!(pdf.contains("/BaseFont /Helvetica"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn embeds_a_subset_when_a_font_is_available() {
|
|
|
|
|
+ if FontFace::load().is_none() {
|
|
|
|
|
+ eprintln!("跳过:本机没有可嵌入的中文字体,走标准 CJK 字体回落");
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ let pdf = render(&facts());
|
|
|
|
|
+ let hay = String::from_utf8_lossy(&pdf[..]);
|
|
|
|
|
+ assert!(hay.contains("/FontFile2"), "应嵌入字体子集");
|
|
|
|
|
+ assert!(hay.contains("/Identity-H"));
|
|
|
|
|
+ assert!(hay.contains("beginbfchar"), "应带 ToUnicode");
|
|
|
|
|
+ assert!(hay.contains("/KLTBOS+"));
|
|
|
|
|
+ // 子集里必须有标题用到的字形:正文不再是 UTF-16,ASCII 行仍是字面量。
|
|
|
|
|
+ assert!(!pdf.windows(8).any(|w| w == "7AE35DE5".as_bytes()));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn literal_escaping() {
|
|
|
|
|
+ assert_eq!(escape_literal("a(b)c\\d"), "a\\(b\\)c\\\\d");
|
|
|
|
|
+ assert_eq!(escape_literal("a\nb"), "a b");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn utf16_hex_of_bmp() {
|
|
|
|
|
+ assert_eq!(utf16_hex("竣工"), "7AE35DE5");
|
|
|
|
|
+ assert_eq!(utf16_hex("JC-1"), "004A0043002D0031");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|