04_export_pdf_and_dxf.js
// 示例 4:导出 PDF / DXF。
//
// 用法:
// node 04_export_pdf_and_dxf.js [mapid] [version]
'use strict';
const { makeClient, sampleMapid, sampleVersion, VjmapError } = require('./_common');
(async () => {
const svc = makeClient();
const mapid = process.argv[2] || sampleMapid();
const version = process.argv[3] || sampleVersion();
console.log(`[1/2] 导出 PDF (mapid=${mapid}/${version})`);
const pdfRes = await svc.cmdExportPdf({
mapid, version, pageWidth: 297, pageHeight: 210, // ODA 纸张 mm;详见 05-compose-export.md
});
console.log(' PDF 导出返回:', pdfRes);
if (pdfRes && pdfRes.path) {
const base = svc.baseGateway().replace(/\/$/, '');
const link = `${base}/${String(pdfRes.path).replace(/^\//, '')}?token=${encodeURIComponent(svc.token)}`;
console.log(' 完整下载链接(可直接浏览器打开):\n ', link);
}
console.log('\n[2/2] 导出 DXF');
try {
const dxfRes = await svc.cmdExportDxf({ mapid, version, cadVersion: '2018' });
console.log(' DXF 导出返回:', dxfRes);
// 注意:exportDxf 的业务失败不抛异常,是返回 { status:false, error }——
// 例如 { error:'Invalid Symbol Table name', status:false } 表示当前图没有可导出图元。
if (dxfRes && dxfRes.status === false) {
console.log(' (业务失败而非异常;exportDxf 整库导出,无裁剪参数)');
}
} catch (e) {
if (e instanceof VjmapError) {
console.log(' DXF 导出失败(业务错误):', e.body);
} else {
throw e;
}
}
})().catch((e) => {
console.error('失败:', e.body || e.message || e);
process.exit(1);
});