Skip to content

Commit 37005a4

Browse files
initial commit
0 parents  commit 37005a4

4 files changed

Lines changed: 172 additions & 0 deletions

File tree

convert.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
param (
2+
[string]$inputPath,
3+
[string]$outputPath,
4+
[string]$keepActive
5+
)
6+
7+
# Convert $keepActive to a boolean
8+
$keepActiveBoolean = $keepActive -eq 'true'
9+
10+
# Check if the input file exists
11+
if (-not (Test-Path $inputPath)) {
12+
Write-Error "Input file does not exist: $inputPath"
13+
exit 1
14+
}
15+
16+
# Your conversion logic here...
17+
# For example, using Word to convert to PDF:
18+
$word = New-Object -ComObject Word.Application
19+
$doc = $word.Documents.Open($inputPath)
20+
$doc.SaveAs([ref]$outputPath, [ref]17) # 17 represents PDF format
21+
$doc.Close()
22+
$word.Quit()
23+
24+
# End of your conversion logic
25+
26+
Write-Host "Conversion completed: $inputPath to $outputPath"

index.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+

package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "docx2pdf-converter",
3+
"version": "1.0.0",
4+
"description": "convert docx file to pdf file",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "npm run test"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Bansnetsajak007/docx2pdf-converter.git"
12+
},
13+
"keywords": [
14+
"docx2pdf"
15+
],
16+
"author": "Sajak",
17+
"license": "ISC",
18+
"bugs": {
19+
"url": "https://github.com/Bansnetsajak007/docx2pdf-converter/issues"
20+
},
21+
"homepage": "https://github.com/Bansnetsajak007/docx2pdf-converter#readme"
22+
}

report.docx

201 KB
Binary file not shown.

0 commit comments

Comments
 (0)