tex2pdf-cli 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +33 -0
- package/cli.js +181 -0
- package/find-latex.js +79 -0
- package/install-latex.js +247 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 harshpreet931
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# tex2pdf-cli
|
|
2
|
+
|
|
3
|
+
Convert TeX files to PDF with zero setup.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npx tex2pdf-cli document.tex
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
LaTeX is automatically installed on first use (~200MB download).
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npm install -g tex2pdf-cli
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Options
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
tex2pdf document.tex [output.pdf] [--engine=ENGINE]
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
| Option | Description |
|
|
26
|
+
|--------|-------------|
|
|
27
|
+
| `--engine=ENGINE` | LaTeX engine: `xelatex` (default), `pdflatex`, `lualatex` |
|
|
28
|
+
| `--install` | Force reinstall TinyTeX |
|
|
29
|
+
| `--help` | Show help |
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
MIT
|
package/cli.js
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const { spawn } = require('child_process');
|
|
6
|
+
const latex = require('node-latex');
|
|
7
|
+
const { hasLaTeX, getEnginePath, findTinyTeX } = require('./find-latex');
|
|
8
|
+
const { fixTinyTeXSymlinks } = require('./install-latex');
|
|
9
|
+
|
|
10
|
+
const args = process.argv.slice(2);
|
|
11
|
+
|
|
12
|
+
function showHelp() {
|
|
13
|
+
console.log(`
|
|
14
|
+
Usage: tex2pdf <input.tex> [output.pdf] [options]
|
|
15
|
+
|
|
16
|
+
Convert TeX files to PDF instantly with zero setup.
|
|
17
|
+
LaTeX is auto-installed on first use if not present.
|
|
18
|
+
|
|
19
|
+
Arguments:
|
|
20
|
+
input.tex Path to your TeX file
|
|
21
|
+
output.pdf Output PDF path (optional, defaults to input.pdf)
|
|
22
|
+
|
|
23
|
+
Options:
|
|
24
|
+
--engine LaTeX engine: pdflatex (default), xelatex, lualatex
|
|
25
|
+
--install Force re/install LaTeX (TinyTeX)
|
|
26
|
+
--help Show this help message
|
|
27
|
+
|
|
28
|
+
Examples:
|
|
29
|
+
tex2pdf document.tex
|
|
30
|
+
tex2pdf document.tex output.pdf
|
|
31
|
+
tex2pdf document.tex --engine=xelatex
|
|
32
|
+
npx tex2pdf-cli document.tex
|
|
33
|
+
|
|
34
|
+
First run may take a few minutes to download LaTeX (~200MB).
|
|
35
|
+
`);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function runInstallScript() {
|
|
39
|
+
console.log('LaTeX not found. Installing TinyTeX automatically...');
|
|
40
|
+
console.log(' This is a one-time setup (~200MB download)');
|
|
41
|
+
console.log('');
|
|
42
|
+
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const installScript = path.join(__dirname, 'install-latex.js');
|
|
45
|
+
const child = spawn('node', [installScript], {
|
|
46
|
+
stdio: 'inherit'
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
child.on('close', (code) => {
|
|
50
|
+
if (code === 0) {
|
|
51
|
+
resolve();
|
|
52
|
+
} else {
|
|
53
|
+
reject(new Error('Installation failed'));
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
child.on('error', (err) => {
|
|
58
|
+
reject(err);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function ensureLaTeX() {
|
|
64
|
+
if (hasLaTeX()) {
|
|
65
|
+
fixTinyTeXSymlinks();
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
await runInstallScript();
|
|
71
|
+
fixTinyTeXSymlinks();
|
|
72
|
+
return hasLaTeX();
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error('\nFailed to install LaTeX automatically.');
|
|
75
|
+
console.error(' You can install it manually from:');
|
|
76
|
+
console.error(' - https://tug.org/texlive/ (TeX Live)');
|
|
77
|
+
console.error(' - https://miktex.org/ (MiKTeX for Windows)');
|
|
78
|
+
console.error(' - https://yihui.org/tinytex/ (TinyTeX)');
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function convertFile(inputPath, outputPath, engine) {
|
|
84
|
+
if (!fs.existsSync(inputPath)) {
|
|
85
|
+
console.error(`Error: File not found: ${inputPath}`);
|
|
86
|
+
process.exit(1);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const enginePath = getEnginePath(engine);
|
|
90
|
+
if (!enginePath) {
|
|
91
|
+
console.error(`Error: LaTeX engine "${engine}" not found`);
|
|
92
|
+
process.exit(1);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const input = fs.createReadStream(inputPath);
|
|
96
|
+
const output = fs.createWriteStream(outputPath);
|
|
97
|
+
|
|
98
|
+
console.log(`Converting ${path.basename(inputPath)}...`);
|
|
99
|
+
|
|
100
|
+
const pdf = latex(input, {
|
|
101
|
+
cmd: enginePath,
|
|
102
|
+
inputs: path.dirname(inputPath)
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
pdf.pipe(output);
|
|
106
|
+
|
|
107
|
+
return new Promise((resolve, reject) => {
|
|
108
|
+
pdf.on('error', (err) => {
|
|
109
|
+
reject(err);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
output.on('finish', () => {
|
|
113
|
+
console.log(`Created: ${outputPath}`);
|
|
114
|
+
resolve();
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function main() {
|
|
120
|
+
if (args.length === 0 || args.includes('--help') || args.includes('-h')) {
|
|
121
|
+
showHelp();
|
|
122
|
+
process.exit(0);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (args.includes('--install')) {
|
|
126
|
+
console.log('Force installing LaTeX...');
|
|
127
|
+
await runInstallScript();
|
|
128
|
+
process.exit(0);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const latexReady = await ensureLaTeX();
|
|
132
|
+
if (!latexReady) {
|
|
133
|
+
process.exit(1);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const inputFile = args[0];
|
|
137
|
+
let outputFile = null;
|
|
138
|
+
let engine = 'xelatex';
|
|
139
|
+
|
|
140
|
+
for (let i = 1; i < args.length; i++) {
|
|
141
|
+
const arg = args[i];
|
|
142
|
+
if (arg.startsWith('--engine=')) {
|
|
143
|
+
engine = arg.split('=')[1];
|
|
144
|
+
} else if (arg.startsWith('--')) {
|
|
145
|
+
console.error(`Unknown option: ${arg}`);
|
|
146
|
+
process.exit(1);
|
|
147
|
+
} else if (!outputFile) {
|
|
148
|
+
outputFile = arg;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (!outputFile) {
|
|
153
|
+
const parsed = path.parse(inputFile);
|
|
154
|
+
outputFile = path.join(parsed.dir, `${parsed.name}.pdf`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
await convertFile(inputFile, outputFile, engine);
|
|
159
|
+
} catch (err) {
|
|
160
|
+
if (engine === 'pdflatex' && err.message.includes('Font')) {
|
|
161
|
+
console.log(`pdflatex failed (font issue), trying xelatex...`);
|
|
162
|
+
try {
|
|
163
|
+
await convertFile(inputFile, outputFile, 'xelatex');
|
|
164
|
+
return;
|
|
165
|
+
} catch (fallbackErr) {
|
|
166
|
+
console.error(`\nConversion failed with both engines:`);
|
|
167
|
+
console.error(fallbackErr.message);
|
|
168
|
+
process.exit(1);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
console.error(`\nConversion failed:`);
|
|
172
|
+
console.error(err.message);
|
|
173
|
+
if (err.message.includes('Command failed')) {
|
|
174
|
+
console.error('\nTip: Your TeX file may have errors or missing packages.');
|
|
175
|
+
console.error(' Check the file compiles with a LaTeX editor first.');
|
|
176
|
+
}
|
|
177
|
+
process.exit(1);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
main();
|
package/find-latex.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const { execSync } = require('child_process');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
|
|
6
|
+
const PLATFORM = os.platform();
|
|
7
|
+
const TINYTEX_DIR = path.join(__dirname, '.tinytex');
|
|
8
|
+
|
|
9
|
+
function findSystemLaTeX() {
|
|
10
|
+
try {
|
|
11
|
+
const pdflatexPath = execSync('which pdflatex || where pdflatex', {
|
|
12
|
+
encoding: 'utf8',
|
|
13
|
+
stdio: ['pipe', 'pipe', 'ignore']
|
|
14
|
+
}).trim();
|
|
15
|
+
|
|
16
|
+
if (pdflatexPath) {
|
|
17
|
+
return path.dirname(pdflatexPath);
|
|
18
|
+
}
|
|
19
|
+
} catch (e) {
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function findTinyTeX() {
|
|
25
|
+
let binPath;
|
|
26
|
+
|
|
27
|
+
if (PLATFORM === 'win32') {
|
|
28
|
+
binPath = path.join(TINYTEX_DIR, 'bin', 'windows');
|
|
29
|
+
} else if (PLATFORM === 'darwin') {
|
|
30
|
+
binPath = path.join(TINYTEX_DIR, 'bin', 'universal-darwin');
|
|
31
|
+
} else {
|
|
32
|
+
binPath = path.join(TINYTEX_DIR, 'bin', 'x86_64-linux');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const pdflatexPath = path.join(binPath, PLATFORM === 'win32' ? 'pdflatex.exe' : 'pdflatex');
|
|
36
|
+
|
|
37
|
+
if (fs.existsSync(pdflatexPath)) {
|
|
38
|
+
return binPath;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function getLaTeXPath() {
|
|
45
|
+
const systemPath = findSystemLaTeX();
|
|
46
|
+
if (systemPath) {
|
|
47
|
+
return { path: systemPath, type: 'system' };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const tinytexPath = findTinyTeX();
|
|
51
|
+
if (tinytexPath) {
|
|
52
|
+
return { path: tinytexPath, type: 'tinytex' };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function hasLaTeX() {
|
|
59
|
+
return getLaTeXPath() !== null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function getEnginePath(engine = 'pdflatex') {
|
|
63
|
+
const latexInfo = getLaTeXPath();
|
|
64
|
+
|
|
65
|
+
if (!latexInfo) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const engineName = PLATFORM === 'win32' ? `${engine}.exe` : engine;
|
|
70
|
+
return path.join(latexInfo.path, engineName);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = {
|
|
74
|
+
getLaTeXPath,
|
|
75
|
+
hasLaTeX,
|
|
76
|
+
getEnginePath,
|
|
77
|
+
findTinyTeX,
|
|
78
|
+
TINYTEX_DIR
|
|
79
|
+
};
|
package/install-latex.js
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
const https = require('https');
|
|
6
|
+
const { execSync } = require('child_process');
|
|
7
|
+
const tar = require('tar');
|
|
8
|
+
const os = require('os');
|
|
9
|
+
|
|
10
|
+
const PLATFORM = os.platform();
|
|
11
|
+
const INSTALL_DIR = path.join(__dirname, '.tinytex');
|
|
12
|
+
|
|
13
|
+
function log(message) {
|
|
14
|
+
console.log(`[tex2pdf-setup] ${message}`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function checkExistingLaTeX() {
|
|
18
|
+
try {
|
|
19
|
+
execSync('pdflatex --version', { stdio: 'ignore' });
|
|
20
|
+
return true;
|
|
21
|
+
} catch (e) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function checkTinyTeXInstalled() {
|
|
27
|
+
const tinytexBin = getTinyTeXBinPath();
|
|
28
|
+
if (!tinytexBin) return false;
|
|
29
|
+
const xelatexPath = path.join(tinytexBin, PLATFORM === 'win32' ? 'xelatex.exe' : 'xelatex');
|
|
30
|
+
return fs.existsSync(xelatexPath);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getTinyTeXBinPath() {
|
|
34
|
+
if (PLATFORM === 'win32') {
|
|
35
|
+
return path.join(INSTALL_DIR, 'bin', 'windows');
|
|
36
|
+
} else if (PLATFORM === 'darwin') {
|
|
37
|
+
return path.join(INSTALL_DIR, 'bin', 'universal-darwin');
|
|
38
|
+
} else {
|
|
39
|
+
return path.join(INSTALL_DIR, 'bin', 'x86_64-linux');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function fixTinyTeXSymlinks() {
|
|
44
|
+
const binPath = getTinyTeXBinPath();
|
|
45
|
+
if (!fs.existsSync(binPath)) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let fixedCount = 0;
|
|
50
|
+
|
|
51
|
+
const files = fs.readdirSync(binPath);
|
|
52
|
+
for (const file of files) {
|
|
53
|
+
const filePath = path.join(binPath, file);
|
|
54
|
+
try {
|
|
55
|
+
const stats = fs.lstatSync(filePath);
|
|
56
|
+
if (stats.isSymbolicLink()) {
|
|
57
|
+
try {
|
|
58
|
+
fs.accessSync(filePath, fs.constants.F_OK);
|
|
59
|
+
} catch (e) {
|
|
60
|
+
fs.unlinkSync(filePath);
|
|
61
|
+
fixedCount++;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
} catch (e) {
|
|
65
|
+
try {
|
|
66
|
+
fs.unlinkSync(filePath);
|
|
67
|
+
fixedCount++;
|
|
68
|
+
} catch (e2) {}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (fixedCount > 0) {
|
|
73
|
+
log(`Fixed ${fixedCount} broken symlinks`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const symlinks = [
|
|
77
|
+
{ from: 'pdftex', to: 'pdflatex' },
|
|
78
|
+
{ from: 'tex', to: 'latex' },
|
|
79
|
+
{ from: 'luatex', to: 'lualatex' },
|
|
80
|
+
{ from: 'xetex', to: 'xelatex' },
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
for (const { from, to } of symlinks) {
|
|
84
|
+
const fromPath = path.join(binPath, PLATFORM === 'win32' ? `${from}.exe` : from);
|
|
85
|
+
const toPath = path.join(binPath, PLATFORM === 'win32' ? `${to}.exe` : to);
|
|
86
|
+
|
|
87
|
+
if (fs.existsSync(fromPath) && !fs.existsSync(toPath)) {
|
|
88
|
+
try {
|
|
89
|
+
fs.symlinkSync(fromPath, toPath);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
try {
|
|
92
|
+
fs.copyFileSync(fromPath, toPath);
|
|
93
|
+
} catch (e2) {}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function downloadFile(url, dest) {
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
log(`Downloading TinyTeX (~200MB)...`);
|
|
104
|
+
const file = fs.createWriteStream(dest);
|
|
105
|
+
|
|
106
|
+
https.get(url, { redirect: 'follow' }, (response) => {
|
|
107
|
+
if (response.statusCode === 301 || response.statusCode === 302) {
|
|
108
|
+
file.close();
|
|
109
|
+
fs.unlinkSync(dest);
|
|
110
|
+
downloadFile(response.headers.location, dest).then(resolve).catch(reject);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (response.statusCode !== 200) {
|
|
115
|
+
reject(new Error(`Download failed with status ${response.statusCode}`));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const totalSize = parseInt(response.headers['content-length'], 10);
|
|
120
|
+
let downloadedSize = 0;
|
|
121
|
+
let lastPercent = 0;
|
|
122
|
+
|
|
123
|
+
response.on('data', (chunk) => {
|
|
124
|
+
downloadedSize += chunk.length;
|
|
125
|
+
if (totalSize) {
|
|
126
|
+
const percent = Math.floor((downloadedSize / totalSize) * 100);
|
|
127
|
+
if (percent > lastPercent && percent % 10 === 0) {
|
|
128
|
+
log(`Download progress: ${percent}%`);
|
|
129
|
+
lastPercent = percent;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
response.pipe(file);
|
|
135
|
+
|
|
136
|
+
file.on('finish', () => {
|
|
137
|
+
file.close();
|
|
138
|
+
resolve();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
file.on('error', (err) => {
|
|
142
|
+
fs.unlinkSync(dest);
|
|
143
|
+
reject(err);
|
|
144
|
+
});
|
|
145
|
+
}).on('error', (err) => {
|
|
146
|
+
fs.unlinkSync(dest);
|
|
147
|
+
reject(err);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async function installTinyTeXUnix() {
|
|
153
|
+
// Use full TinyTeX (not TinyTeX-1) which includes format files
|
|
154
|
+
const url = PLATFORM === 'darwin'
|
|
155
|
+
? 'https://github.com/rstudio/tinytex-releases/releases/download/daily/TinyTeX.tgz'
|
|
156
|
+
: 'https://github.com/rstudio/tinytex-releases/releases/download/daily/TinyTeX.tar.gz';
|
|
157
|
+
const tarPath = path.join(__dirname, `tinytex-${Date.now()}.tar.gz`);
|
|
158
|
+
|
|
159
|
+
try {
|
|
160
|
+
await downloadFile(url, tarPath);
|
|
161
|
+
|
|
162
|
+
log('Extracting TinyTeX...');
|
|
163
|
+
if (!fs.existsSync(INSTALL_DIR)) {
|
|
164
|
+
fs.mkdirSync(INSTALL_DIR, { recursive: true });
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
await tar.extract({
|
|
168
|
+
file: tarPath,
|
|
169
|
+
cwd: INSTALL_DIR,
|
|
170
|
+
strip: 1
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
fs.unlinkSync(tarPath);
|
|
174
|
+
fixTinyTeXSymlinks();
|
|
175
|
+
|
|
176
|
+
return true;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
log(`Error installing TinyTeX: ${error.message}`);
|
|
179
|
+
if (fs.existsSync(tarPath)) {
|
|
180
|
+
fs.unlinkSync(tarPath);
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async function installTinyTeXWindows() {
|
|
187
|
+
log('Windows installation requires manual steps or Chocolatey');
|
|
188
|
+
log('Please install TinyTeX using one of these methods:');
|
|
189
|
+
log(' 1. choco install tinytex');
|
|
190
|
+
log(' 2. scoop install tinytex');
|
|
191
|
+
log(' 3. Download from https://github.com/rstudio/tinytex-releases');
|
|
192
|
+
log('');
|
|
193
|
+
log('Or use WSL (Windows Subsystem for Linux) for automatic installation');
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function installTinyTeX() {
|
|
198
|
+
log('LaTeX not found. Installing TinyTeX (this may take a few minutes)...');
|
|
199
|
+
|
|
200
|
+
if (PLATFORM === 'win32') {
|
|
201
|
+
return await installTinyTeXWindows();
|
|
202
|
+
} else {
|
|
203
|
+
return await installTinyTeXUnix();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async function main() {
|
|
208
|
+
const isPostinstall = process.env.npm_lifecycle_event === 'postinstall';
|
|
209
|
+
|
|
210
|
+
log('Checking for LaTeX installation...');
|
|
211
|
+
|
|
212
|
+
if (checkExistingLaTeX()) {
|
|
213
|
+
log('Found existing LaTeX installation. Skipping TinyTeX installation.');
|
|
214
|
+
process.exit(0);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (checkTinyTeXInstalled()) {
|
|
218
|
+
fixTinyTeXSymlinks();
|
|
219
|
+
log('TinyTeX is already installed and ready.');
|
|
220
|
+
process.exit(0);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const success = await installTinyTeX();
|
|
224
|
+
|
|
225
|
+
if (success) {
|
|
226
|
+
log('TinyTeX installed successfully!');
|
|
227
|
+
log(`Location: ${INSTALL_DIR}`);
|
|
228
|
+
process.exit(0);
|
|
229
|
+
} else {
|
|
230
|
+
log('Failed to install TinyTeX automatically.');
|
|
231
|
+
log('LaTeX will be installed on first use of tex2pdf command.');
|
|
232
|
+
log('Or install manually from https://tug.org/texlive/ or https://miktex.org/');
|
|
233
|
+
|
|
234
|
+
if (isPostinstall) {
|
|
235
|
+
log('Note: This is non-fatal. npm install will continue.');
|
|
236
|
+
process.exit(0);
|
|
237
|
+
} else {
|
|
238
|
+
process.exit(1);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
module.exports = { fixTinyTeXSymlinks, getTinyTeXBinPath };
|
|
244
|
+
|
|
245
|
+
if (require.main === module) {
|
|
246
|
+
main();
|
|
247
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tex2pdf-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Convert TeX files to PDF with zero setup - auto-installs LaTeX if needed",
|
|
5
|
+
"main": "cli.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"tex2pdf": "./cli.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"cli.js",
|
|
11
|
+
"find-latex.js",
|
|
12
|
+
"install-latex.js",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"postinstall": "node install-latex.js || true",
|
|
18
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"tex",
|
|
22
|
+
"latex",
|
|
23
|
+
"pdf",
|
|
24
|
+
"converter",
|
|
25
|
+
"cli",
|
|
26
|
+
"npx",
|
|
27
|
+
"tinytex",
|
|
28
|
+
"zero-setup"
|
|
29
|
+
],
|
|
30
|
+
"author": "Your Name <your.email@example.com>",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/yourusername/tex2pdf-cli.git"
|
|
35
|
+
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/yourusername/tex2pdf-cli/issues"
|
|
38
|
+
},
|
|
39
|
+
"homepage": "https://github.com/yourusername/tex2pdf-cli#readme",
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"node-latex": "^3.1.0",
|
|
42
|
+
"tar": "^7.0.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=14.0.0"
|
|
46
|
+
}
|
|
47
|
+
}
|