Skip to content

Commit 7c16900

Browse files
committed
feat: 🎉 iniciando repositorio e adicionando arquivos do projeto
0 parents  commit 7c16900

File tree

6 files changed

+363
-0
lines changed

6 files changed

+363
-0
lines changed

.gitignore

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
2+
3+
# Logs
4+
5+
logs
6+
_.log
7+
npm-debug.log_
8+
yarn-debug.log*
9+
yarn-error.log*
10+
lerna-debug.log*
11+
.pnpm-debug.log*
12+
13+
# Caches
14+
15+
.cache
16+
17+
# Diagnostic reports (https://nodejs.org/api/report.html)
18+
19+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
20+
21+
# Runtime data
22+
23+
pids
24+
_.pid
25+
_.seed
26+
*.pid.lock
27+
28+
# Directory for instrumented libs generated by jscoverage/JSCover
29+
30+
lib-cov
31+
32+
# Coverage directory used by tools like istanbul
33+
34+
coverage
35+
*.lcov
36+
37+
# nyc test coverage
38+
39+
.nyc_output
40+
41+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
42+
43+
.grunt
44+
45+
# Bower dependency directory (https://bower.io/)
46+
47+
bower_components
48+
49+
# node-waf configuration
50+
51+
.lock-wscript
52+
53+
# Compiled binary addons (https://nodejs.org/api/addons.html)
54+
55+
build/Release
56+
57+
# Dependency directories
58+
59+
node_modules/
60+
jspm_packages/
61+
62+
# Snowpack dependency directory (https://snowpack.dev/)
63+
64+
web_modules/
65+
66+
# TypeScript cache
67+
68+
*.tsbuildinfo
69+
70+
# Optional npm cache directory
71+
72+
.npm
73+
74+
# Optional eslint cache
75+
76+
.eslintcache
77+
78+
# Optional stylelint cache
79+
80+
.stylelintcache
81+
82+
# Microbundle cache
83+
84+
.rpt2_cache/
85+
.rts2_cache_cjs/
86+
.rts2_cache_es/
87+
.rts2_cache_umd/
88+
89+
# Optional REPL history
90+
91+
.node_repl_history
92+
93+
# Output of 'npm pack'
94+
95+
*.tgz
96+
97+
# Yarn Integrity file
98+
99+
.yarn-integrity
100+
101+
# dotenv environment variable files
102+
103+
.env
104+
.env.development.local
105+
.env.test.local
106+
.env.production.local
107+
.env.local
108+
109+
# parcel-bundler cache (https://parceljs.org/)
110+
111+
.parcel-cache
112+
113+
# Next.js build output
114+
115+
.next
116+
out
117+
118+
# Nuxt.js build / generate output
119+
120+
.nuxt
121+
dist
122+
123+
# Gatsby files
124+
125+
# Comment in the public line in if your project uses Gatsby and not Next.js
126+
127+
# https://nextjs.org/blog/next-9-1#public-directory-support
128+
129+
# public
130+
131+
# vuepress build output
132+
133+
.vuepress/dist
134+
135+
# vuepress v2.x temp and cache directory
136+
137+
.temp
138+
139+
# Docusaurus cache and generated files
140+
141+
.docusaurus
142+
143+
# Serverless directories
144+
145+
.serverless/
146+
147+
# FuseBox cache
148+
149+
.fusebox/
150+
151+
# DynamoDB Local files
152+
153+
.dynamodb/
154+
155+
# TernJS port file
156+
157+
.tern-port
158+
159+
# Stores VSCode versions used for testing VSCode extensions
160+
161+
.vscode-test
162+
163+
# yarn v2
164+
165+
.yarn/cache
166+
.yarn/unplugged
167+
.yarn/build-state.yml
168+
.yarn/install-state.gz
169+
.pnp.*
170+
171+
# IntelliJ based IDEs
172+
.idea
173+
174+
# Finder (MacOS) folder config
175+
.DS_Store

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# pdfserver
2+
3+
To install dependencies:
4+
5+
```bash
6+
bun install
7+
```
8+
9+
To run:
10+
11+
```bash
12+
bun run index.ts
13+
```
14+
15+
This project was created using `bun init` in bun v1.1.10. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

bun.lockb

45 KB
Binary file not shown.

index.ts

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import Bun from "bun";
2+
import puppeteer from "puppeteer";
3+
4+
type HandlerParams = { request: Request; url: URL };
5+
type Handler = ({ request, url }: HandlerParams) => Promise<Response>;
6+
7+
const listagemProdutosUrl = "http://localhost:3000/relatorio/listagem/produtos";
8+
9+
async function relatorioListagemProdutos(
10+
{ request, url }: HandlerParams,
11+
): Promise<Response> {
12+
console.info(Date.now(), "[RELATORIO] ListagemProdutos");
13+
const htmlContent = `
14+
<html>
15+
<head>
16+
<title>Relatório de Produtos</title>
17+
</head>
18+
<body>
19+
<!-- <h1>Relatório de Produtos</h1> -->
20+
<table border="1" cellpadding="5" cellspacing="0.5" width="100%">
21+
<thead>
22+
<tr>
23+
${
24+
`
25+
<th>ID</th>
26+
<th>Nome</th>
27+
<th>Preço</th>
28+
`.repeat(4)
29+
}
30+
</tr>
31+
</thead>
32+
<tbody>
33+
${
34+
`
35+
<tr>
36+
${
37+
`
38+
<td>1</td>
39+
<td>Produto A</td>
40+
<td>R$ 10,00</td>
41+
`.repeat(4)
42+
}
43+
</tr>
44+
<tr>
45+
${
46+
`
47+
<td>2</td>
48+
<td>Produto B</td>
49+
<td>R$ 20,00</td>
50+
`.repeat(4)
51+
}
52+
</tr>`.repeat(100)
53+
}
54+
</tbody>
55+
</table>
56+
</body>
57+
</html>
58+
`;
59+
return new Response(htmlContent, {
60+
headers: { "content-type": "text/html;charset=utf-8" },
61+
});
62+
}
63+
64+
async function relatorioListagemProdutosPDF(
65+
{ request, url }: HandlerParams,
66+
): Promise<Response> {
67+
const inicio = Date.now();
68+
console.info(inicio, "[RELATORIO] listagemProdutosPDF #begin");
69+
try {
70+
const response = await fetch(listagemProdutosUrl);
71+
const htmlContent = await response.text();
72+
73+
const browser = await puppeteer.launch({ headless: true });
74+
const page = await browser.newPage();
75+
await page.setContent(htmlContent, { waitUntil: "networkidle0" });
76+
const pdf = await page.pdf({
77+
format: "A4",
78+
margin: { top: "64px", left: "15px", bottom: "32px", right: "15px" },
79+
displayHeaderFooter: true,
80+
headerTemplate: `
81+
<div style="display:flex;align-items: baseline;justify-content: space-between;width:100%">
82+
<div>
83+
<span style="margin-left:32px;font-size:24px"> Listagem de Produtos </span>
84+
</div>
85+
<div style="margin-right:32px;font-size:12px">
86+
<span class="date"></span> - Página <span class="pageNumber"></span> de <span class="totalPages"></span>
87+
</div>
88+
</div>`,
89+
footerTemplate: `
90+
<div style="font-size:10px; text-align: center; width: 100%;">
91+
<span class="title"></span>
92+
</div>`,
93+
});
94+
await browser.close();
95+
96+
return new Response(pdf, {
97+
headers: {
98+
"Content-Type": "application/pdf",
99+
},
100+
});
101+
} catch (error) {
102+
console.error(error);
103+
return new Response("Erro ao gerar o PDF", { status: 500 });
104+
} finally {
105+
const fim = Date.now();
106+
console.info(
107+
fim,
108+
"[RELATORIO] listagemProdutosPDF #end -",
109+
fim - inicio,
110+
"ms",
111+
);
112+
}
113+
}
114+
115+
const app = Bun.serve({
116+
async fetch(request) {
117+
const url = new URL(request.url);
118+
const handler: Handler | undefined = {
119+
"/relatorio/listagem/produtos": relatorioListagemProdutos,
120+
"/relatorio/listagem/produtos/pdf": relatorioListagemProdutosPDF,
121+
}[url.pathname];
122+
123+
if (!handler) {
124+
return new Response("nao encontrado", { status: 404 });
125+
}
126+
127+
return await handler({ request, url });
128+
},
129+
});
130+
131+
console.info(`ouvindo requisicoes em http://${app.hostname}:${app.port}`);

package.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "pdfserver",
3+
"module": "index.ts",
4+
"type": "module",
5+
"devDependencies": {
6+
"@types/bun": "latest",
7+
"bun-types": "^1.1.12"
8+
},
9+
"peerDependencies": {
10+
"typescript": "^5.0.0"
11+
},
12+
"dependencies": {
13+
"puppeteer": "^22.10.0"
14+
}
15+
}

tsconfig.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"compilerOptions": {
3+
// Enable latest features
4+
"lib": ["ESNext", "DOM"],
5+
"target": "ESNext",
6+
"module": "ESNext",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
11+
// Bundler mode
12+
"moduleResolution": "bundler",
13+
"allowImportingTsExtensions": true,
14+
"verbatimModuleSyntax": true,
15+
"noEmit": true,
16+
17+
// Best practices
18+
"strict": true,
19+
"skipLibCheck": true,
20+
"noFallthroughCasesInSwitch": true,
21+
22+
// Some stricter flags (disabled by default)
23+
"noUnusedLocals": false,
24+
"noUnusedParameters": false,
25+
"noPropertyAccessFromIndexSignature": false
26+
}
27+
}

0 commit comments

Comments
 (0)