function latin1(str) { const out = new Uint8Array(str.length); for (let i = 0; i < str.length; i += 1) out[i] = str.charCodeAt(i) & 0xff; return out; } function concatBytes(parts) { let len = 0; for (let i = 0; i < parts.length; i += 1) len += parts[i].length; const out = new Uint8Array(len); let offset = 0; for (let i = 0; i < parts.length; i += 1) { out.set(parts[i], offset); offset += parts[i].length; } return out; } function readU16(bytes, offset) { return (bytes[offset] << 8) | bytes[offset + 1]; } function parseJpegSize(bytes) { if (!bytes || bytes.length < 4 || bytes[0] !== 0xff || bytes[1] !== 0xd8) { return { width: 1240, height: 1754 }; } let i = 2; while (i + 8 < bytes.length) { if (bytes[i] !== 0xff) { i += 1; continue; } const marker = bytes[i + 1]; if (marker === 0xd9 || marker === 0xda) break; const size = readU16(bytes, i + 2); if ( (marker >= 0xc0 && marker <= 0xc3) || (marker >= 0xc5 && marker <= 0xc7) || (marker >= 0xc9 && marker <= 0xcb) || (marker >= 0xcd && marker <= 0xcf) ) { return { height: readU16(bytes, i + 5), width: readU16(bytes, i + 7), }; } i += 2 + size; } return { width: 1240, height: 1754 }; } /** 将多张 JPEG 包成 A4 竖向 PDF(每页一张图,无需中文字体)。 */ function buildJpegPdf(jpegList, pageWidth = 595.28, pageHeight = 841.89) { const images = (jpegList || []).filter((buf) => buf && buf.length); if (!images.length) throw new Error('没有可打印的页面'); const chunks = []; let pos = 0; const push = (u8) => { chunks.push(u8); pos += u8.length; }; const pushStr = (s) => push(latin1(s)); const offsets = []; const startObj = () => { offsets.push(pos); }; pushStr('%PDF-1.4\n%\x80\x80\x80\x80\n'); const pageCount = images.length; const pageObj = (i) => 3 + i * 3; const contentObj = (i) => 4 + i * 3; const imageObj = (i) => 5 + i * 3; startObj(); pushStr('1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n'); startObj(); const kids = images.map((_, i) => `${pageObj(i)} 0 R`).join(' '); pushStr(`2 0 obj\n<< /Type /Pages /Kids [${kids}] /Count ${pageCount} >>\nendobj\n`); for (let i = 0; i < images.length; i += 1) { const jpeg = images[i] instanceof Uint8Array ? images[i] : new Uint8Array(images[i]); startObj(); pushStr( `${pageObj(i)} 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 ${pageWidth} ${pageHeight}] /Resources << /XObject << /Im0 ${imageObj(i)} 0 R >> >> /Contents ${contentObj(i)} 0 R >>\nendobj\n` ); const content = `q ${pageWidth} 0 0 ${pageHeight} 0 0 cm /Im0 Do Q\n`; startObj(); pushStr(`${contentObj(i)} 0 obj\n<< /Length ${content.length} >>\nstream\n`); pushStr(content); pushStr('endstream\nendobj\n'); const { width, height } = parseJpegSize(jpeg); startObj(); pushStr( `${imageObj(i)} 0 obj\n<< /Type /XObject /Subtype /Image /Width ${width} /Height ${height} /ColorSpace /DeviceRGB /BitsPerComponent 8 /Filter /DCTDecode /Length ${jpeg.length} >>\nstream\n` ); push(jpeg); pushStr('\nendstream\nendobj\n'); } const xrefPos = pos; pushStr(`xref\n0 ${3 + pageCount * 3}\n`); pushStr('0000000000 65535 f \n'); for (let i = 0; i < offsets.length; i += 1) { pushStr(`${String(offsets[i]).padStart(10, '0')} 00000 n \n`); } pushStr(`trailer\n<< /Size ${3 + pageCount * 3} /Root 1 0 R >>\nstartxref\n${xrefPos}\n%%EOF\n`); return concatBytes(chunks); } function readFileBytes(filePath) { return new Promise((resolve, reject) => { wx.getFileSystemManager().readFile({ filePath, success: (res) => { const data = res.data; if (data instanceof ArrayBuffer) { resolve(new Uint8Array(data)); return; } if (data instanceof Uint8Array) { resolve(data); return; } reject(new Error('读取图片失败')); }, fail: (err) => reject(new Error(err.errMsg || '读取图片失败')), }); }); } function writePdfFile(filePath, bytes) { return new Promise((resolve, reject) => { wx.getFileSystemManager().writeFile({ filePath, data: bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength), success: resolve, fail: (err) => reject(new Error(err.errMsg || '保存 PDF 失败')), }); }); } function openPdf(filePath) { return new Promise((resolve, reject) => { wx.openDocument({ filePath, fileType: 'pdf', showMenu: true, success: resolve, fail: (err) => reject(new Error(err.errMsg || '打开 PDF 失败')), }); }); } module.exports = { buildJpegPdf, parseJpegSize, readFileBytes, writePdfFile, openPdf, };