react-pdf-levelup 3.1.59 → 3.2.7
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/README.md +132 -36
- package/dist/index.cjs +4 -1132
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +59 -11
- package/dist/index.d.ts +59 -11
- package/dist/index.js +4 -1040
- package/dist/index.js.map +1 -0
- package/package.json +17 -19
- package/license +0 -17
package/README.md
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://genarogg.github.io/media/react-pdf-levelup/logo-de-react-pdf-levelup.png" alt="react-pdf-levelup logo" width="160" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
1
5
|
# react-pdf-levelup
|
|
2
6
|
|
|
3
7
|
Generador de PDFs dinámicos con React. Esta herramienta te permite crear plantillas PDF con componentes JSX personalizados y previsualizarlas en tiempo real dentro de una aplicación web. Ideal para etiquetas, facturas, reportes, certificados, tablas y más.
|
|
4
8
|
|
|
5
9
|
# 🌐 **Playground en vivo:**
|
|
6
|
-
[https://react-pdf-levelup.
|
|
10
|
+
[https://react-pdf-levelup.nimbux.cloud](https://react-pdf-levelup.nimbux.cloud)
|
|
11
|
+
or
|
|
12
|
+
[https://react-pdf-levelup.nimbux.cloud](https://react-pdf-levelup.nimbux.cloud)
|
|
7
13
|
|
|
8
14
|
## 📦 Instalación
|
|
9
15
|
|
|
@@ -13,7 +19,7 @@ npm install react-pdf-levelup
|
|
|
13
19
|
|
|
14
20
|
## 🚀 Características
|
|
15
21
|
|
|
16
|
-
- 🧱 Construye PDFs con componentes de React usando
|
|
22
|
+
- 🧱 Construye PDFs con componentes de React usando los componentes de `react-pdf-levelup` (LayoutPDF, texto, listas, QR, tablas, columnas, etc.)
|
|
17
23
|
- 🖼 Vista previa en tiempo real de los documentos generados
|
|
18
24
|
- 🎨 Editor en vivo con Monaco Editor para personalizar código JSX
|
|
19
25
|
- 📦 Plantillas predefinidas listas para usar
|
|
@@ -68,43 +74,53 @@ decodeBase64Pdf(pdfBase64, 'mi-documento.pdf');
|
|
|
68
74
|
- Abre el PDF en una nueva pestaña del navegador
|
|
69
75
|
- Limpia automáticamente los recursos de memoria
|
|
70
76
|
|
|
71
|
-
## 💡 Ejemplo de Uso
|
|
77
|
+
## 💡 Ejemplo de Uso con componentes levelup
|
|
72
78
|
|
|
73
79
|
```typescript
|
|
74
80
|
import React from 'react';
|
|
75
|
-
import {
|
|
76
|
-
import { generatePDF, decodeBase64Pdf } from 'react-pdf-levelup';
|
|
77
|
-
|
|
78
|
-
// Definir estilos para el PDF
|
|
79
|
-
const styles = StyleSheet.create({
|
|
80
|
-
page: {
|
|
81
|
-
flexDirection: 'column',
|
|
82
|
-
backgroundColor: '#E4E4E4',
|
|
83
|
-
padding: 30,
|
|
84
|
-
},
|
|
85
|
-
section: {
|
|
86
|
-
margin: 10,
|
|
87
|
-
padding: 10,
|
|
88
|
-
flexGrow: 1,
|
|
89
|
-
},
|
|
90
|
-
title: {
|
|
91
|
-
fontSize: 24,
|
|
92
|
-
marginBottom: 10,
|
|
93
|
-
},
|
|
94
|
-
});
|
|
81
|
+
import { generatePDF, decodeBase64Pdf, LayoutPDF, H1, P, Strong, Em, HR, Container, Row, Col6, UL, LI, QR, Table, Thead, Tbody, Tr, Th, Td } from 'react-pdf-levelup';
|
|
95
82
|
|
|
96
|
-
// Crear template del PDF
|
|
97
83
|
const MyPDFTemplate = ({ data }) => (
|
|
98
|
-
<
|
|
99
|
-
<
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
84
|
+
<LayoutPDF>
|
|
85
|
+
<H1>Documento de Presentación</H1>
|
|
86
|
+
<P>
|
|
87
|
+
Bienvenido a <Strong>react-pdf-levelup</Strong>. Construye PDFs con componentes de React de forma <Em>rápida</Em> y <Em>tipada</Em>.
|
|
88
|
+
</P>
|
|
89
|
+
<HR />
|
|
90
|
+
<Container>
|
|
91
|
+
<Row>
|
|
92
|
+
<Col6>
|
|
93
|
+
<UL>
|
|
94
|
+
<LI>Plantillas listas</LI>
|
|
95
|
+
<LI>Componentes composables</LI>
|
|
96
|
+
<LI>TypeScript</LI>
|
|
97
|
+
<LI>Integración moderna</LI>
|
|
98
|
+
</UL>
|
|
99
|
+
</Col6>
|
|
100
|
+
<Col6>
|
|
101
|
+
<QR value="https://react-pdf-levelup.nimbux.cloud" size={120} />
|
|
102
|
+
</Col6>
|
|
103
|
+
</Row>
|
|
104
|
+
</Container>
|
|
105
|
+
<Table cellHeight={24}>
|
|
106
|
+
<Thead>
|
|
107
|
+
<Tr>
|
|
108
|
+
<Th width="40%">Producto</Th>
|
|
109
|
+
<Th width="20%">Cantidad</Th>
|
|
110
|
+
<Th width="20%">Precio</Th>
|
|
111
|
+
<Th width="20%">Total</Th>
|
|
112
|
+
</Tr>
|
|
113
|
+
</Thead>
|
|
114
|
+
<Tbody>
|
|
115
|
+
<Tr>
|
|
116
|
+
<Td width="40%">Etiqueta Térmica 50x30</Td>
|
|
117
|
+
<Td width="20%">2</Td>
|
|
118
|
+
<Td width="20%">$5.00</Td>
|
|
119
|
+
<Td width="20%">$10.00</Td>
|
|
120
|
+
</Tr>
|
|
121
|
+
</Tbody>
|
|
122
|
+
</Table>
|
|
123
|
+
</LayoutPDF>
|
|
108
124
|
);
|
|
109
125
|
|
|
110
126
|
// Función para generar y descargar PDF
|
|
@@ -231,14 +247,94 @@ Esta librería utiliza internamente:
|
|
|
231
247
|
## 📝 Notas Importantes
|
|
232
248
|
|
|
233
249
|
- La función `decodeBase64Pdf` solo funciona en contexto de navegador (requiere `document`)
|
|
234
|
-
- Los templates deben
|
|
250
|
+
- Los templates deben usar los componentes de `react-pdf-levelup` y retornar JSX válido
|
|
235
251
|
- El PDF se genera de forma asíncrona, asegúrate de usar `await` o `.then()`
|
|
236
252
|
- Los recursos de memoria se limpian automáticamente después de la descarga
|
|
237
253
|
|
|
254
|
+
## 🌐 API REST para generar PDFs
|
|
255
|
+
|
|
256
|
+
- Genera PDFs vía HTTP desde cualquier lenguaje usando un template TSX en base64 y un objeto de datos.
|
|
257
|
+
- Devuelve un JSON con `data.pdf` que es el PDF en base64.
|
|
258
|
+
|
|
259
|
+
### Endpoints
|
|
260
|
+
|
|
261
|
+
- Cloud: [https://react-pdf-levelup.nimbux.cloud/api](https://react-pdf-levelup.nimbux.cloud/api)
|
|
262
|
+
- Auto‑hospedado ZIP: [https://genarogg.github.io/react-pdf-levelup/public/api.zip](https://genarogg.github.io/react-pdf-levelup/public/api.zip)
|
|
263
|
+
|
|
264
|
+
```text
|
|
265
|
+
https://react-pdf-levelup.nimbux.cloud/api
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
```text
|
|
269
|
+
https://genarogg.github.io/react-pdf-levelup/public/api.zip
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### Request
|
|
273
|
+
|
|
274
|
+
POST con `Content-Type: application/json`:
|
|
275
|
+
|
|
276
|
+
```json
|
|
277
|
+
{
|
|
278
|
+
"template": "<TSX_EN_BASE64>",
|
|
279
|
+
"data": { "campo": "valor" }
|
|
280
|
+
}
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Response
|
|
284
|
+
|
|
285
|
+
```json
|
|
286
|
+
{
|
|
287
|
+
"data": {
|
|
288
|
+
"pdf": "<PDF_EN_BASE64>"
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Ejemplo rápido con Node.js (fetch)
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
import fs from "fs";
|
|
297
|
+
import path from "path";
|
|
298
|
+
|
|
299
|
+
type ApiResponse = { data?: { pdf?: string } };
|
|
300
|
+
const ENDPOINT_API = "https://react-pdf-levelup.nimbux.cloud/api";
|
|
301
|
+
|
|
302
|
+
const petition = async ({ template, data }: { template: string, data: any }): Promise<string> => {
|
|
303
|
+
const templatePath = path.join(process.cwd(), "src", "useExample", template);
|
|
304
|
+
const tsxCode = fs.readFileSync(templatePath, "utf-8");
|
|
305
|
+
const templateBase64 = Buffer.from(tsxCode, "utf-8").toString("base64");
|
|
306
|
+
|
|
307
|
+
const res = await fetch(ENDPOINT_API, {
|
|
308
|
+
method: "POST",
|
|
309
|
+
headers: { "Content-Type": "application/json" },
|
|
310
|
+
body: JSON.stringify({ template: templateBase64, data }),
|
|
311
|
+
});
|
|
312
|
+
if (!res.ok) throw new Error(`API error (${res.status}): ${await res.text()}`);
|
|
313
|
+
const json = await res.json() as ApiResponse;
|
|
314
|
+
return json?.data?.pdf ?? "";
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const savePDF = (base64: string) => {
|
|
318
|
+
const buffer = Buffer.from(base64, "base64");
|
|
319
|
+
const outputPath = path.join(process.cwd(), "example.pdf");
|
|
320
|
+
fs.writeFileSync(outputPath, buffer);
|
|
321
|
+
console.log("PDF guardado:", outputPath);
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
### Self‑hosting propio
|
|
326
|
+
|
|
327
|
+
- Descarga el paquete ZIP y despliega en tu infraestructura (Node/Docker/PaaS).
|
|
328
|
+
- Expón el endpoint `/api/pdf` con el mismo contrato JSON.
|
|
329
|
+
- Usa el mismo cliente mostrado arriba apuntando a tu URL.
|
|
330
|
+
|
|
331
|
+
Más detalles y ejemplos en la documentación:
|
|
332
|
+
[Guía API REST (fetch)](https://react-pdf-levelup-docs.nimbux.cloud/docs/guides/api-rest)
|
|
333
|
+
|
|
238
334
|
## 🤝 Contribuir
|
|
239
335
|
|
|
240
336
|
Las contribuciones son bienvenidas. Por favor, abre un issue o envía un pull request.
|
|
241
337
|
|
|
242
338
|
## 📄 Licencia
|
|
243
339
|
|
|
244
|
-
MIT License
|
|
340
|
+
MIT License
|