|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const assert = require('assert'); |
| 5 | + |
| 6 | +const packageVersion = require('./package.json').version; |
| 7 | + |
| 8 | +/** |
| 9 | + * Convert Word document to PDF on Windows using PowerShell |
| 10 | + */ |
| 11 | +function windows(inputPath, outputPath, keepActive) { |
| 12 | + if (!inputPath) { |
| 13 | + console.error('Input path is not provided.'); |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + const scriptPath = path.resolve(__dirname, 'convert.ps1'); |
| 18 | + const inputFilePath = path.resolve(inputPath); |
| 19 | + const outputFilePath = path.resolve(outputPath); |
| 20 | + |
| 21 | + const command = `powershell -File "${scriptPath}" "${inputFilePath}" "${outputFilePath}" ${keepActive ? 'true' : 'false'}`; |
| 22 | + |
| 23 | + execSync(command); |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | +/** |
| 28 | + * Resolve input and output paths |
| 29 | + */ |
| 30 | +function resolvePaths(inputPath, outputPath) { |
| 31 | + if (!inputPath) { |
| 32 | + console.error('Input path is not provided.'); |
| 33 | + process.exit(1); // Exit with an error code |
| 34 | + } |
| 35 | + |
| 36 | + const inputFilePath = path.resolve(inputPath); |
| 37 | + let outputFilePath = outputPath ? path.resolve(outputPath) : null; |
| 38 | + |
| 39 | + const output = {}; |
| 40 | + |
| 41 | + if (!fs.existsSync(inputFilePath)) { |
| 42 | + console.error('Input file does not exist:', inputFilePath); |
| 43 | + process.exit(1); // Exit with an error code |
| 44 | + } |
| 45 | + |
| 46 | + if (fs.statSync(inputFilePath).isDirectory()) { |
| 47 | + output.batch = true; |
| 48 | + output.input = inputFilePath; |
| 49 | + |
| 50 | + if (outputPath) { |
| 51 | + if (!fs.existsSync(outputPath) || !fs.statSync(outputPath).isDirectory()) { |
| 52 | + console.error('Output path is not a valid directory:', outputPath); |
| 53 | + process.exit(1); // Exit with an error code |
| 54 | + } |
| 55 | + } else { |
| 56 | + outputPath = inputFilePath; |
| 57 | + } |
| 58 | + |
| 59 | + output.output = outputPath; |
| 60 | + } else { |
| 61 | + output.batch = false; |
| 62 | + assert(inputFilePath.endsWith('.docx')); |
| 63 | + output.input = inputFilePath; |
| 64 | + |
| 65 | + if (outputPath && fs.statSync(outputPath).isDirectory()) { |
| 66 | + outputFilePath = path.resolve(outputPath, `${path.basename(inputFilePath, '.docx')}.pdf`); |
| 67 | + } else if (outputPath && outputPath.endsWith('.pdf')) { |
| 68 | + // outputPath is a file path |
| 69 | + outputFilePath = outputPath; |
| 70 | + } else { |
| 71 | + outputFilePath = path.resolve(path.dirname(inputFilePath), `${path.basename(inputFilePath, '.docx')}.pdf`); |
| 72 | + } |
| 73 | + |
| 74 | + output.output = outputFilePath; |
| 75 | + } |
| 76 | + |
| 77 | + return output; |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +/** |
| 82 | + * Convert Word document to PDF on macOS using a shell script |
| 83 | + */ |
| 84 | +function macos(inputPath, outputPath, keepActive) { |
| 85 | + if (!inputPath) { |
| 86 | + console.error('Input path is not provided.'); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const scriptPath = path.resolve(__dirname, 'convert.sh'); |
| 91 | + const inputFilePath = path.resolve(inputPath); |
| 92 | + const outputFilePath = outputPath ? path.resolve(outputPath) : null; |
| 93 | + |
| 94 | + const command = `sh "${scriptPath}" "${inputFilePath}" "${outputFilePath}" ${keepActive ? 'true' : 'false'}`; |
| 95 | + |
| 96 | + execSync(command); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Convert Word document to PDF using platform-specific method |
| 101 | + */ |
| 102 | +function convert(inputPath, outputPath, keepActive = false) { |
| 103 | + if (process.platform === 'darwin') { |
| 104 | + macos(inputPath, outputPath, keepActive); |
| 105 | + } else { |
| 106 | + windows(inputPath, outputPath, keepActive); |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +module.exports = { |
| 111 | + convert, |
| 112 | + resolvePaths, |
| 113 | + windows, |
| 114 | + macos, |
| 115 | + packageVersion, |
| 116 | + }; |
| 117 | + |
| 118 | + // const inputPath = 'report.docx'; // Adjust this based on the actual filename |
| 119 | + // const outputPath = 'output.pdf'; // Adjust this based on the desired output filename |
| 120 | + // const keepActive = false; |
| 121 | + |
| 122 | + // windows(inputPath, outputPath, keepActive); |
| 123 | + |
| 124 | + |
0 commit comments