1
0
Fork 0
freesewing/packages/svgtopdf/src/index.js

89 lines
3 KiB
JavaScript
Raw Normal View History

2018-12-20 16:54:05 +01:00
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
2018-12-20 16:54:05 +01:00
import path from "path";
import fs from "fs";
2018-12-20 16:54:05 +01:00
import formidable from "formidable";
import shellExec from "shell-exec";
const app = express();
app.use(cors());
app.use(bodyParser.json());
2018-12-20 16:54:05 +01:00
const port = process.env.PORT || 4000;
2018-12-20 16:59:29 +01:00
const formats = ['pdf','ps'];
const sizes = ['full', 'a4','a3','a2','a1','a0','letter','tabloid'];
2018-12-20 16:54:05 +01:00
app.get("/", async (req, res) => res.sendFile(path.resolve(__dirname+'/form.html')));
app.post("/", async (req, res) => {
let form = new formidable.IncomingForm();
form.parse(req, (err, fields, files) => {
2018-12-20 16:59:29 +01:00
if(err || typeof files.svg === "undefined" ||
2018-12-20 16:54:05 +01:00
formats.indexOf(fields.format) === -1 ||
sizes.indexOf(fields.size) === -1
) return res.sendFile(path.resolve(__dirname+'/form.html'));
let upload = files.svg.path;
let cmd;
if(fields.size === "full") { // Do not tile
let target = `/tmp/pattern.${fields.format}`;
cmd = `/usr/bin/inkscape --export-${fields.format}=${target} ${upload}`;
shellExec(cmd).then(() => {
return res.sendFile(target);
});
} else { // Do tile
let untiled = "/tmp/untiled.ps";
let tiled = "/tmp/tiled.ps";
cmd = `/usr/bin/inkscape --export-ps=${untiled} ${upload}`;
shellExec(cmd).then(() => {
cmd = `/usr/local/bin/tile -a -m${fields.size} -s1 -t"On-demand tiler" ${untiled} > ${tiled}`;
shellExec(cmd).then(() => {
if(fields.format === "ps") return res.sendFile(tiled);
cmd = `/usr/bin/ps2pdf14 ${tiled} ${tiled}.pdf`;
shellExec(cmd).then(() => {
return res.sendFile(tiled+'.pdf');
});
});
});
}
});
});
app.post("/api", async (req, res) => {
if(
typeof req.body.svg === "undefined" ||
typeof req.body.format === "undefined" ||
2019-01-05 19:00:41 +00:00
typeof req.body.size === "undefined" ||
formats.indexOf(req.body.format) === -1 ||
sizes.indexOf(req.body.size) === -1
)
return res.sendStatus(400);
// Save svg to disk
2019-01-05 19:00:41 +00:00
fs.writeFile("/tmp/draft.svg", req.body.svg, err => {
if(err) return res.sendStatus(500);
let cmd;
2019-01-05 19:00:41 +00:00
if(req.body.size === "full") { // Do not tile
let target = "/tmp/pattern.pdf";
cmd = "/usr/bin/inkscape --export-pdf="+target+" /tmp/draft.svg";
shellExec(cmd).then(() => {
return res.sendFile(target);
});
} else { // Do tile
let untiled = "/tmp/untiled.ps";
let tiled = "/tmp/tiled.ps";
cmd = `/usr/bin/inkscape --export-ps=${untiled} /tmp/draft.svg`;
shellExec(cmd).then(() => {
cmd = `/usr/local/bin/tile -a -m${req.size} -s1 -t"On-demand tiler" ${untiled} > ${tiled}`;
shellExec(cmd).then(() => {
if(req.format === "ps") return res.sendFile(tiled);
cmd = `/usr/bin/ps2pdf14 ${tiled} ${tiled}.pdf`;
shellExec(cmd).then(() => {
return res.sendFile(tiled+'.pdf');
});
});
});
}
});
});
2018-12-20 16:59:29 +01:00
app.listen(port, err => { console.log(`> listening on port ${port}`) });