324 lines
8.2 KiB
TypeScript
324 lines
8.2 KiB
TypeScript
import * as fs from "fs";
|
|
import {
|
|
Document, Packer, Paragraph, PageOrientation, ImageRun,
|
|
HorizontalPositionRelativeFrom, VerticalPositionRelativeFrom,
|
|
convertMillimetersToTwip,
|
|
FrameAnchorType,
|
|
HorizontalPositionAlign,
|
|
VerticalPositionAlign,
|
|
TextRun,
|
|
} from "docx";
|
|
import { join } from "path";
|
|
|
|
|
|
function cm(centimeters: number) {
|
|
return convertMillimetersToTwip((100 * centimeters) / 150);
|
|
}
|
|
|
|
function cmToEmu(cm: number) {
|
|
return Math.round(cm * 360000);
|
|
}
|
|
|
|
type ImgConfig = {
|
|
name: string,
|
|
height: number,
|
|
width: number,
|
|
horizontalOffset: number,
|
|
verticalOffset: number,
|
|
}
|
|
|
|
function getImage(data: ImgConfig): ImageRun {
|
|
return new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", data.name)),
|
|
transformation: {
|
|
height: cm(data.height),
|
|
width: cm(data.width),
|
|
},
|
|
floating: {
|
|
zIndex: 0,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(data.horizontalOffset),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(data.verticalOffset),
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
const imgFondoDoc = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "fondo_certificado.png")),
|
|
transformation: {
|
|
height: cm(20.97),
|
|
width: cm(29.8),
|
|
},
|
|
floating: {
|
|
zIndex: 0,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: 0,
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: 0,
|
|
},
|
|
},
|
|
});
|
|
|
|
|
|
const imgMatpel = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "matpel-logo.png")),
|
|
transformation: {
|
|
height: cm(2.81),
|
|
width: cm(2.81),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(0.7),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(0.7),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgCIP = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "colegio_ingenieros_logo.png")),
|
|
transformation: {
|
|
height: cm(2.15),
|
|
width: cm(2.15),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(0.91),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(3.64),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgCEE = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "cee_logo.png")),
|
|
transformation: {
|
|
height: cm(2.22),
|
|
width: cm(3.11),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(0.29),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(18),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgMTC = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "mtc_logo.png")),
|
|
transformation: {
|
|
height: cm(1.3),
|
|
width: cm(4.08),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(5.13),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(19.33),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgEEG = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "eeg_f_logo.png")),
|
|
transformation: {
|
|
height: cm(4.39),
|
|
width: cm(6.4),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(23.25),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(0),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgOSHA = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "osha_logo.png")),
|
|
transformation: {
|
|
height: cm(1.55),
|
|
width: cm(4.46),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(24.34),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(4.95),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgAguila = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "aguila_logo.png")),
|
|
transformation: {
|
|
height: cm(2.62),
|
|
width: cm(2.68),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(25.45),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(6.57),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgMichigan = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "michigan_logo.png")),
|
|
transformation: {
|
|
height: cm(2.47),
|
|
width: cm(2.6),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(25.59),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(9.54),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgCEEM = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "ceem_logo.jpg")),
|
|
transformation: {
|
|
height: cm(1),
|
|
width: cm(3.99),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(24.97),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(12.5),
|
|
},
|
|
},
|
|
});
|
|
|
|
const imgEATE = new ImageRun({
|
|
data: fs.readFileSync(join(__dirname, "img", "eate_logo.jpg")),
|
|
transformation: {
|
|
height: cm(2.21),
|
|
width: cm(3.28),
|
|
},
|
|
floating: {
|
|
zIndex: 1,
|
|
horizontalPosition: {
|
|
relative: HorizontalPositionRelativeFrom.LEFT_MARGIN,
|
|
offset: cmToEmu(25.54),
|
|
},
|
|
verticalPosition: {
|
|
relative: VerticalPositionRelativeFrom.TOP_MARGIN,
|
|
offset: cmToEmu(14.09),
|
|
},
|
|
},
|
|
});
|
|
|
|
const certificateName = new Paragraph({
|
|
frame: {
|
|
position: {
|
|
x: cmToEmu(3.13),
|
|
y: cmToEmu(1.39),
|
|
},
|
|
width: cm(14.28) * 5,
|
|
height: cm(3.19) * 5,
|
|
anchor: {
|
|
horizontal: FrameAnchorType.MARGIN,
|
|
vertical: FrameAnchorType.MARGIN,
|
|
},
|
|
alignment: {
|
|
x: HorizontalPositionAlign.CENTER,
|
|
y: VerticalPositionAlign.TOP,
|
|
},
|
|
},
|
|
children: [
|
|
new TextRun({
|
|
text: "sample text",
|
|
bold: true,
|
|
font: "Arial",
|
|
}),
|
|
],
|
|
});
|
|
|
|
|
|
const doc = new Document({
|
|
sections: [
|
|
{
|
|
properties: {
|
|
page: {
|
|
size: {
|
|
orientation: PageOrientation.LANDSCAPE,
|
|
},
|
|
},
|
|
},
|
|
children: [
|
|
certificateName,
|
|
new Paragraph({
|
|
children: [
|
|
imgFondoDoc,
|
|
imgMatpel,
|
|
imgCIP,
|
|
imgCEE,
|
|
imgMTC,
|
|
imgEEG,
|
|
imgOSHA,
|
|
imgAguila,
|
|
imgMichigan,
|
|
imgCEEM,
|
|
imgEATE,
|
|
],
|
|
}),
|
|
],
|
|
},
|
|
],
|
|
});
|
|
|
|
// Used to export the file into a .docx file
|
|
Packer.toBuffer(doc).then((buffer) => {
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
});
|