This commit is contained in:
TUTOR03 2020-12-13 17:23:49 +05:00
commit 03dfa117b2
12 changed files with 155 additions and 21 deletions

View File

@ -15,7 +15,7 @@
},
"rules": {
"no-console": "warn",
"prettier/prettier": "error"
"prettier/prettier": "warn"
},
"settings": {
"react": {

4
.gitignore vendored
View File

@ -18,4 +18,6 @@ componentName.json
localName.json
yarn.lock
package-lock.json
package-lock.json
test.pdf

View File

@ -15,9 +15,9 @@
},
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/preset-env": "^7.12.10",
"@babel/preset-react": "^7.12.10",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@types/node": "^14.14.12",
"@types/react": "^15.0.0 || ^16.0.0 || ^17.0.0 ",
"@types/react-dom": "^17.0.0",
@ -41,8 +41,9 @@
"webpack-dev-server": "^3.11.0"
},
"dependencies": {
"@types/pdfmake": "^0.1.16",
"pdfmake": "^0.1.68",
"react": "^15.0.0 || ^16.0.0 || ^17.0.0 ",
"react-dom": "^15.0.0 || ^16.0.0 || ^17.0.0 ",
"serve": "^11.3.2"
"react-dom": "^15.0.0 || ^16.0.0 || ^17.0.0 "
}
}

View File

@ -1,10 +1,12 @@
import React from "react";
import styles from "./styles/App.module.css";
import { createDocument } from "./utils/pdf";
const App: React.FC = () => {
return (
<div>
<h1 className={styles.title}>Hello, world</h1>
<button onClick={() => createDocument(11, 18)}>Create</button>
</div>
);
};

4
src/custom.d.ts vendored
View File

@ -7,3 +7,7 @@ declare module "*.module.css" {
const classes: { [key: string]: string };
export default classes;
}
declare module "*.ttf" {
const content: string;
export default content;
}

BIN
src/fonts/PTSerif-Bold.ttf Normal file

Binary file not shown.

Binary file not shown.

View File

@ -2,6 +2,6 @@
color: red;
}
/*just test sunlime merge*/
body{
background-color: blue;
}
body {
background-color: blue;
}

114
src/utils/pdf.ts Normal file
View File

@ -0,0 +1,114 @@
import pdfMake from "pdfmake/build/pdfmake";
import { TableCell, TDocumentDefinitions } from "pdfmake/interfaces";
import vfs from "./vfsFonts";
pdfMake.vfs = vfs;
pdfMake.fonts = {
PTSerif: {
normal: "PTSerifRegular.ttf",
bold: "PTSerifBold.ttf",
},
};
const months = [
"Января",
"Февраля",
"Марта",
"Апреля",
"Мая",
"Июня",
"Июля",
"Августа",
"Сентября",
"Октября",
"Ноября",
"Декабря",
];
const getDate = () => {
const now = new Date();
return `${now.getDate()} ${months[now.getDay()]} ${now.getFullYear()} года`;
};
const getGroupNumber = (gen: number): number => {
while (gen > 9) {
gen -= 9;
}
return gen;
};
export const createDocument = (
classNumber: number,
generation: number
): void => {
const doc: TDocumentDefinitions = {
content: [
{
text: "УТВЕРЖДЕН",
style: "ral",
},
{
text: "приказом БОУ «Югорский физико-математический лицей-интернат»",
style: "ral",
},
{
text: `№ от ${getDate()}`,
style: "ral",
margin: [0, 0, 0, 10],
},
{
table: {
widths: [
"4.37%",
"4.37%",
"8.8%",
"13.7%",
"13.7%",
"13.7%",
"13.7%",
"13.7%",
"13.7%",
],
body: [
[
{ text: "Пара", rowSpan: 2, margin: [0, 10, 0, 0] },
{ text: "Урок", rowSpan: 2, margin: [0, 10, 0, 0] },
{ text: "Время", rowSpan: 2, margin: [0, 10, 0, 0] },
...["А", "Б", "В"].flatMap<TableCell>(word => [
{
colSpan: 2,
text: `${classNumber} «${word}» класс`,
},
{},
]),
],
[
"",
"",
"",
...[1, 2, 3, 4, 5, 6].map<TableCell>(groupIndex => ({
text: `${getGroupNumber(generation)}${groupIndex} группа`,
})),
],
],
},
},
],
defaultStyle: {
font: "PTSerif",
fontSize: 12,
alignment: "center",
},
styles: {
ral: {
alignment: "right",
},
},
pageOrientation: "landscape",
};
pdfMake.createPdf(doc).download("test.pdf");
};

12
src/utils/vfsFonts.ts Normal file
View File

@ -0,0 +1,12 @@
import regular from "../fonts/PTSerif-Regular.ttf";
import bold from "../fonts/PTSerif-Bold.ttf";
import pdfFonts from "pdfmake/build/vfs_fonts";
pdfFonts.pdfMake.vfs["PTSerifRegular.ttf"] = regular.slice(
regular.indexOf(",")
);
pdfFonts.pdfMake.vfs["PTSerifBold.ttf"] = bold.slice(bold.indexOf(","));
export default pdfFonts.pdfMake.vfs;

View File

@ -8,6 +8,7 @@
"module": "es6",
"moduleResolution": "node",
"target": "es5",
"lib": ["ES2020"],
"allowJs": true,
"jsx": "react",
"allowSyntheticDefaultImports": true

View File

@ -2,7 +2,6 @@
const webpack = require("webpack");
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const ESLintPlugin = require("eslint-webpack-plugin");
const getScopedName = require("./utils/getScopedName.js");
@ -13,17 +12,16 @@ const plugins = [
new HtmlWebpackPlugin({
template: path.join(__dirname, "public", "index.html"),
}),
]
];
if (!isDev)
{
if (!isDev) {
plugins.push(
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify('production')
}
"process.env": {
NODE_ENV: JSON.stringify("production"),
},
})
)
);
}
module.exports = {
@ -39,11 +37,11 @@ module.exports = {
include: path.resolve(__dirname, "src"),
},
{
test:/\.js$/,
test: /\.js$/,
include: /src/,
use:{
loader:"babel-loader"
}
use: {
loader: "babel-loader",
},
},
{
test: /\.css$/,
@ -76,7 +74,7 @@ module.exports = {
],
},
optimization: {
minimize: true,
minimize: !isDev,
},
resolve: {
extensions: [".tsx", ".ts", ".js"],