Files
2026-07-13 19:57:46 +08:00

84 lines
3.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const os = require('node:os');
const path = require('node:path');
const { normalizeSealPlacementHtml, preparePdfRenderSource, pdfPathForSource } = require('../erp_pdf_conversion');
test('preparePdfRenderSource copies Word-compatible HTML .doc files to .html for Chromium rendering', async (t) => {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'erp-pdf-'));
t.after(() => fs.rm(tmp, { recursive: true, force: true }));
const sourcePath = path.join(tmp, 'LW-260722A-A_confirm.doc');
const html = '<html><head><meta charset="utf-8"></head><body>确认单</body></html>';
await fs.writeFile(sourcePath, html, 'utf8');
const prepared = await preparePdfRenderSource(sourcePath, {
renderDir: path.join(tmp, 'render-html'),
});
assert.equal(prepared.originalPath, sourcePath);
assert.equal(prepared.isHtml, true);
assert.notEqual(prepared.renderPath, sourcePath);
assert.equal(path.extname(prepared.renderPath), '.html');
assert.equal(await fs.readFile(prepared.renderPath, 'utf8'), html);
});
test('preparePdfRenderSource leaves non-HTML .doc files unchanged', async (t) => {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'erp-pdf-'));
t.after(() => fs.rm(tmp, { recursive: true, force: true }));
const sourcePath = path.join(tmp, 'binary.doc');
await fs.writeFile(sourcePath, Buffer.from([0xd0, 0xcf, 0x11, 0xe0, 0x00]));
const prepared = await preparePdfRenderSource(sourcePath, {
renderDir: path.join(tmp, 'render-html'),
});
assert.equal(prepared.originalPath, sourcePath);
assert.equal(prepared.renderPath, sourcePath);
assert.equal(prepared.isHtml, false);
});
test('normalizeSealPlacementHtml shifts absolute ERP seal spans into the printable page', () => {
const html = [
'<html><body>',
'<span style="position: absolute;z-index:251660288;margin-left:365px;margin-top:200px;width:168px; height:96px">',
'<img width=168 height=96 src="https://ltjt.yunzhi.run/Images/bei2.png" alt=zhang>',
'</span>',
'</body></html>',
].join('');
const normalized = normalizeSealPlacementHtml(html);
assert.match(normalized, /margin-left:140px/i);
assert.match(normalized, /bei2\.png/);
});
test('normalizeSealPlacementHtml anchors Xingyou seal near first-page party B block', () => {
const html = [
'<html><body>',
'<p>乙方:海南星游国际旅行社有限公司</p>',
'<p>乙方联系方式0898-12345678</p>',
'<div style="height:1200px"></div>',
'<span style="position:absolute;z-index:251660288;margin-left:365px;margin-top:1200px;width:168px;height:96px">',
'<img width=168 height=96 src="https://ltjt.yunzhi.run/Images/xy.png" alt=zhang>',
'</span>',
'</body></html>',
].join('');
const normalized = normalizeSealPlacementHtml(html, { sealAnchor: 'party-b-first-page' });
assert.match(normalized, /data-erp-seal-normalized="party-b-first-page"/);
assert.match(normalized, /margin-top:0/i);
assert.match(normalized, /vertical-align:middle/i);
assert.ok(normalized.indexOf('xy.png') > normalized.indexOf('乙方联系方式'));
});
test('pdfPathForSource replaces document or html extensions with .pdf', () => {
assert.equal(pdfPathForSource('C:\\tmp\\order.doc'), 'C:\\tmp\\order.pdf');
assert.equal(pdfPathForSource('C:\\tmp\\order.html'), 'C:\\tmp\\order.pdf');
assert.equal(pdfPathForSource('C:\\tmp\\order.htm'), 'C:\\tmp\\order.pdf');
});