pdf: add new feature
Signed-off-by: Rodrigo Bertoldo de Sousa Martins <rodrigo.sousa.577@gmail.com>
This commit is contained in:
1 parent
a63aaeb655
commit
61a1631519
14 files changed
+3091
-1
No files matched your search
@@ -0,0 +1,26 @@
|
|||||||
|
# Configurações de Manipulação de PDF
|
||||||
|
|
||||||
|
# Tamanho máximo de arquivo em MB
|
||||||
|
PDF_MAX_FILE_SIZE=100
|
||||||
|
|
||||||
|
# Número máximo de arquivos por operação
|
||||||
|
PDF_MAX_FILES_COMPRESS=10
|
||||||
|
PDF_MAX_FILES_MERGE=50
|
||||||
|
PDF_MAX_FILES_IMAGES_TO_PDF=100
|
||||||
|
|
||||||
|
# Tempo de retenção de arquivos processados em horas
|
||||||
|
PDF_FILES_RETENTION_HOURS=24
|
||||||
|
|
||||||
|
# Qualidade padrão para compressão (50-100)
|
||||||
|
PDF_DEFAULT_QUALITY=95
|
||||||
|
|
||||||
|
# DPI padrão para conversão de imagens (150-600)
|
||||||
|
PDF_DEFAULT_DPI=300
|
||||||
|
|
||||||
|
# Idioma padrão para OCR (por, eng, spa)
|
||||||
|
PDF_DEFAULT_OCR_LANGUAGE=por
|
||||||
|
|
||||||
|
# Diretórios de armazenamento (relativos ao root do projeto)
|
||||||
|
PDF_TEMP_DIR=storage/temp/pdf-ops
|
||||||
|
PDF_OUTPUT_DIR=storage/pdf-output
|
||||||
|
PDF_UPLOAD_DIR=storage/temp/uploads
|
||||||
@@ -5,6 +5,7 @@ import usuariosRoutes from "./routes/usuarios";
|
|||||||
import clientesRoutes from "./routes/clientes";
|
import clientesRoutes from "./routes/clientes";
|
||||||
import processosRoutes from "./routes/processos";
|
import processosRoutes from "./routes/processos";
|
||||||
import audienciasRoutes from "./routes/audiencias";
|
import audienciasRoutes from "./routes/audiencias";
|
||||||
|
import pdfRoutes from "./routes/pdf";
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
@@ -16,6 +17,7 @@ app.use("/api/usuarios", usuariosRoutes);
|
|||||||
app.use("/api/clientes", clientesRoutes);
|
app.use("/api/clientes", clientesRoutes);
|
||||||
app.use("/api/processos", processosRoutes);
|
app.use("/api/processos", processosRoutes);
|
||||||
app.use("/api/audiencias", audienciasRoutes);
|
app.use("/api/audiencias", audienciasRoutes);
|
||||||
|
app.use("/api/pdf", pdfRoutes);
|
||||||
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
res.json({ message: "API is running" });
|
res.json({ message: "API is running" });
|
||||||
|
|||||||
@@ -0,0 +1,798 @@
|
|||||||
|
import { Request, Response } from 'express';
|
||||||
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
import { PDFService } from '../services/pdfService';
|
||||||
|
import multer from 'multer';
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs/promises';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
// Configuração do multer para upload de arquivos
|
||||||
|
const storage = multer.diskStorage({
|
||||||
|
destination: async (req, file, cb) => {
|
||||||
|
const uploadDir = path.join(process.cwd(), 'storage', 'temp', 'uploads');
|
||||||
|
await fs.mkdir(uploadDir, { recursive: true });
|
||||||
|
cb(null, uploadDir);
|
||||||
|
},
|
||||||
|
filename: (req, file, cb) => {
|
||||||
|
const uniqueName = `${uuidv4()}${path.extname(file.originalname)}`;
|
||||||
|
cb(null, uniqueName);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const upload = multer({
|
||||||
|
storage,
|
||||||
|
limits: {
|
||||||
|
fileSize: 100 * 1024 * 1024, // 100MB
|
||||||
|
},
|
||||||
|
fileFilter: (req, file, cb) => {
|
||||||
|
const allowedMimes = [
|
||||||
|
'application/pdf',
|
||||||
|
'image/png',
|
||||||
|
'image/jpeg',
|
||||||
|
'image/jpg',
|
||||||
|
'image/webp',
|
||||||
|
];
|
||||||
|
|
||||||
|
if (allowedMimes.includes(file.mimetype)) {
|
||||||
|
cb(null, true);
|
||||||
|
} else {
|
||||||
|
cb(new Error('Tipo de arquivo não permitido. Use PDF ou imagens (PNG, JPG, WEBP).'));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export const uploadMiddleware = upload;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller para operações de PDF
|
||||||
|
*/
|
||||||
|
export class PDFController {
|
||||||
|
/**
|
||||||
|
* Comprime um ou mais PDFs
|
||||||
|
*/
|
||||||
|
static async compress(req: Request, res: Response): Promise<void> {
|
||||||
|
const startTime = Date.now();
|
||||||
|
let operacaoId: string | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const files = req.files as Express.Multer.File[];
|
||||||
|
const userId = (req as any).user?.id;
|
||||||
|
|
||||||
|
if (!files || files.length === 0) {
|
||||||
|
res.status(400).json({ error: 'Nenhum arquivo enviado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const quality = req.body.quality ? parseInt(req.body.quality) : 95;
|
||||||
|
const maxSizeMB = req.body.maxSizeMB ? parseInt(req.body.maxSizeMB) : undefined;
|
||||||
|
|
||||||
|
const arquivosOriginais: string[] = [];
|
||||||
|
const arquivosResultado: string[] = [];
|
||||||
|
let tamanhoOriginal = BigInt(0);
|
||||||
|
let tamanhoResultado = BigInt(0);
|
||||||
|
|
||||||
|
// Cria registro da operação no banco
|
||||||
|
const operacao = await prisma.operacaoPDF.create({
|
||||||
|
data: {
|
||||||
|
tipoOperacao: 'COMPRIMIR',
|
||||||
|
status: 'PROCESSANDO',
|
||||||
|
usuarioId: userId,
|
||||||
|
arquivosOriginais: [],
|
||||||
|
arquivosResultado: [],
|
||||||
|
tamanhoOriginal: BigInt(0),
|
||||||
|
parametros: { quality, maxSizeMB },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
operacaoId = operacao.id;
|
||||||
|
|
||||||
|
// Processa cada arquivo
|
||||||
|
const results = [];
|
||||||
|
for (const file of files) {
|
||||||
|
arquivosOriginais.push(file.path);
|
||||||
|
tamanhoOriginal += BigInt(file.size);
|
||||||
|
|
||||||
|
const result = await PDFService.compressPDF(file.path, undefined, {
|
||||||
|
quality,
|
||||||
|
maxSizeMB,
|
||||||
|
});
|
||||||
|
|
||||||
|
arquivosResultado.push(result.outputPath);
|
||||||
|
tamanhoResultado += BigInt(result.compressedSize);
|
||||||
|
|
||||||
|
results.push({
|
||||||
|
originalName: file.originalname,
|
||||||
|
originalSize: result.originalSize,
|
||||||
|
compressedSize: result.compressedSize,
|
||||||
|
reduction: result.reduction,
|
||||||
|
outputPath: result.outputPath,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove o arquivo original
|
||||||
|
await fs.unlink(file.path).catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
|
const tempoProcessamento = Date.now() - startTime;
|
||||||
|
|
||||||
|
// Atualiza a operação no banco
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'CONCLUIDO',
|
||||||
|
arquivosOriginais,
|
||||||
|
arquivosResultado,
|
||||||
|
tamanhoOriginal,
|
||||||
|
tamanhoResultado,
|
||||||
|
tempoProcessamento,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
operacaoId,
|
||||||
|
results,
|
||||||
|
totalOriginalSize: Number(tamanhoOriginal),
|
||||||
|
totalCompressedSize: Number(tamanhoResultado),
|
||||||
|
totalReduction: ((Number(tamanhoOriginal) - Number(tamanhoResultado)) / Number(tamanhoOriginal)) * 100,
|
||||||
|
processingTime: tempoProcessamento,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao comprimir PDF:', error);
|
||||||
|
|
||||||
|
if (operacaoId) {
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'ERRO',
|
||||||
|
mensagemErro: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
tempoProcessamento: Date.now() - startTime,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao comprimir PDF',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Divide um PDF
|
||||||
|
*/
|
||||||
|
static async split(req: Request, res: Response): Promise<void> {
|
||||||
|
const startTime = Date.now();
|
||||||
|
let operacaoId: string | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const file = (req.files as Express.Multer.File[])?.[0];
|
||||||
|
const userId = (req as any).user?.id;
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
res.status(400).json({ error: 'Nenhum arquivo enviado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { splitType, pages, ranges, chunkSize } = req.body;
|
||||||
|
|
||||||
|
if (!splitType) {
|
||||||
|
res.status(400).json({ error: 'Tipo de divisão não especificado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const options: any = { splitType };
|
||||||
|
|
||||||
|
if (splitType === 'pages' && pages) {
|
||||||
|
options.pages = JSON.parse(pages);
|
||||||
|
} else if (splitType === 'range' && ranges) {
|
||||||
|
options.ranges = JSON.parse(ranges);
|
||||||
|
} else if (splitType === 'chunks' && chunkSize) {
|
||||||
|
options.chunkSize = parseInt(chunkSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cria registro da operação
|
||||||
|
const operacao = await prisma.operacaoPDF.create({
|
||||||
|
data: {
|
||||||
|
tipoOperacao: 'DIVIDIR',
|
||||||
|
status: 'PROCESSANDO',
|
||||||
|
usuarioId: userId,
|
||||||
|
arquivosOriginais: [file.path],
|
||||||
|
arquivosResultado: [],
|
||||||
|
tamanhoOriginal: BigInt(file.size),
|
||||||
|
parametros: options,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
operacaoId = operacao.id;
|
||||||
|
|
||||||
|
// Divide o PDF
|
||||||
|
const outputPaths = await PDFService.splitPDF(file.path, options);
|
||||||
|
|
||||||
|
// Calcula tamanho total dos arquivos resultantes
|
||||||
|
let tamanhoResultado = BigInt(0);
|
||||||
|
for (const outputPath of outputPaths) {
|
||||||
|
const stats = await fs.stat(outputPath);
|
||||||
|
tamanhoResultado += BigInt(stats.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tempoProcessamento = Date.now() - startTime;
|
||||||
|
|
||||||
|
// Atualiza a operação
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'CONCLUIDO',
|
||||||
|
arquivosResultado: outputPaths,
|
||||||
|
tamanhoResultado,
|
||||||
|
tempoProcessamento,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove o arquivo original
|
||||||
|
await fs.unlink(file.path).catch(() => {});
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
operacaoId,
|
||||||
|
filesCreated: outputPaths.length,
|
||||||
|
outputPaths,
|
||||||
|
processingTime: tempoProcessamento,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao dividir PDF:', error);
|
||||||
|
|
||||||
|
if (operacaoId) {
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'ERRO',
|
||||||
|
mensagemErro: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
tempoProcessamento: Date.now() - startTime,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao dividir PDF',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mescla múltiplos PDFs
|
||||||
|
*/
|
||||||
|
static async merge(req: Request, res: Response): Promise<void> {
|
||||||
|
const startTime = Date.now();
|
||||||
|
let operacaoId: string | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const files = req.files as Express.Multer.File[];
|
||||||
|
const userId = (req as any).user?.id;
|
||||||
|
|
||||||
|
if (!files || files.length < 2) {
|
||||||
|
res.status(400).json({ error: 'São necessários pelo menos 2 arquivos para mesclar' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const filePaths = files.map((f) => f.path);
|
||||||
|
let tamanhoOriginal = BigInt(0);
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
tamanhoOriginal += BigInt(file.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cria registro da operação
|
||||||
|
const operacao = await prisma.operacaoPDF.create({
|
||||||
|
data: {
|
||||||
|
tipoOperacao: 'MESCLAR',
|
||||||
|
status: 'PROCESSANDO',
|
||||||
|
usuarioId: userId,
|
||||||
|
arquivosOriginais: filePaths,
|
||||||
|
arquivosResultado: [],
|
||||||
|
tamanhoOriginal,
|
||||||
|
parametros: { filesCount: files.length },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
operacaoId = operacao.id;
|
||||||
|
|
||||||
|
// Mescla os PDFs
|
||||||
|
const outputPath = await PDFService.mergePDFs({
|
||||||
|
filePaths,
|
||||||
|
outputPath: path.join(process.cwd(), 'storage', 'pdf-output', `merged-${uuidv4()}.pdf`),
|
||||||
|
});
|
||||||
|
|
||||||
|
// Calcula tamanho do arquivo resultante
|
||||||
|
const stats = await fs.stat(outputPath);
|
||||||
|
const tamanhoResultado = BigInt(stats.size);
|
||||||
|
|
||||||
|
const tempoProcessamento = Date.now() - startTime;
|
||||||
|
|
||||||
|
// Atualiza a operação
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'CONCLUIDO',
|
||||||
|
arquivosResultado: [outputPath],
|
||||||
|
tamanhoResultado,
|
||||||
|
tempoProcessamento,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove os arquivos originais
|
||||||
|
for (const filePath of filePaths) {
|
||||||
|
await fs.unlink(filePath).catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
operacaoId,
|
||||||
|
outputPath,
|
||||||
|
originalSize: Number(tamanhoOriginal),
|
||||||
|
mergedSize: Number(tamanhoResultado),
|
||||||
|
processingTime: tempoProcessamento,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao mesclar PDFs:', error);
|
||||||
|
|
||||||
|
if (operacaoId) {
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'ERRO',
|
||||||
|
mensagemErro: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
tempoProcessamento: Date.now() - startTime,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao mesclar PDFs',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converte PDF em imagens
|
||||||
|
*/
|
||||||
|
static async pdfToImages(req: Request, res: Response): Promise<void> {
|
||||||
|
const startTime = Date.now();
|
||||||
|
let operacaoId: string | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const file = (req.files as Express.Multer.File[])?.[0];
|
||||||
|
const userId = (req as any).user?.id;
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
res.status(400).json({ error: 'Nenhum arquivo enviado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const format = req.body.format || 'png';
|
||||||
|
const quality = req.body.quality ? parseInt(req.body.quality) : 95;
|
||||||
|
const density = req.body.density ? parseInt(req.body.density) : 300;
|
||||||
|
const pages = req.body.pages ? JSON.parse(req.body.pages) : 'all';
|
||||||
|
|
||||||
|
// Cria registro da operação
|
||||||
|
const operacao = await prisma.operacaoPDF.create({
|
||||||
|
data: {
|
||||||
|
tipoOperacao: 'PDF_PARA_IMAGEM',
|
||||||
|
status: 'PROCESSANDO',
|
||||||
|
usuarioId: userId,
|
||||||
|
arquivosOriginais: [file.path],
|
||||||
|
arquivosResultado: [],
|
||||||
|
tamanhoOriginal: BigInt(file.size),
|
||||||
|
parametros: { format, quality, density, pages },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
operacaoId = operacao.id;
|
||||||
|
|
||||||
|
// Converte para imagens
|
||||||
|
const outputPaths = await PDFService.pdfToImages(file.path, {
|
||||||
|
format,
|
||||||
|
quality,
|
||||||
|
density,
|
||||||
|
pages,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Calcula tamanho total
|
||||||
|
let tamanhoResultado = BigInt(0);
|
||||||
|
for (const outputPath of outputPaths) {
|
||||||
|
const stats = await fs.stat(outputPath);
|
||||||
|
tamanhoResultado += BigInt(stats.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
const tempoProcessamento = Date.now() - startTime;
|
||||||
|
|
||||||
|
// Atualiza a operação
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'CONCLUIDO',
|
||||||
|
arquivosResultado: outputPaths,
|
||||||
|
tamanhoResultado,
|
||||||
|
tempoProcessamento,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove o arquivo original
|
||||||
|
await fs.unlink(file.path).catch(() => {});
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
operacaoId,
|
||||||
|
imagesCreated: outputPaths.length,
|
||||||
|
outputPaths,
|
||||||
|
processingTime: tempoProcessamento,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao converter PDF para imagens:', error);
|
||||||
|
|
||||||
|
if (operacaoId) {
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'ERRO',
|
||||||
|
mensagemErro: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
tempoProcessamento: Date.now() - startTime,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao converter PDF para imagens',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converte imagens em PDF
|
||||||
|
*/
|
||||||
|
static async imagesToPDF(req: Request, res: Response): Promise<void> {
|
||||||
|
const startTime = Date.now();
|
||||||
|
let operacaoId: string | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const files = req.files as Express.Multer.File[];
|
||||||
|
const userId = (req as any).user?.id;
|
||||||
|
|
||||||
|
if (!files || files.length === 0) {
|
||||||
|
res.status(400).json({ error: 'Nenhuma imagem enviada' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const imagePaths = files.map((f) => f.path);
|
||||||
|
let tamanhoOriginal = BigInt(0);
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
tamanhoOriginal += BigInt(file.size);
|
||||||
|
}
|
||||||
|
|
||||||
|
const fitToPage = req.body.fitToPage === 'true';
|
||||||
|
const pageSize = req.body.pageSize ? JSON.parse(req.body.pageSize) : undefined;
|
||||||
|
|
||||||
|
// Cria registro da operação
|
||||||
|
const operacao = await prisma.operacaoPDF.create({
|
||||||
|
data: {
|
||||||
|
tipoOperacao: 'IMAGEM_PARA_PDF',
|
||||||
|
status: 'PROCESSANDO',
|
||||||
|
usuarioId: userId,
|
||||||
|
arquivosOriginais: imagePaths,
|
||||||
|
arquivosResultado: [],
|
||||||
|
tamanhoOriginal,
|
||||||
|
parametros: { fitToPage, pageSize, imagesCount: files.length },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
operacaoId = operacao.id;
|
||||||
|
|
||||||
|
// Converte para PDF
|
||||||
|
const outputPath = await PDFService.imagesToPDF({
|
||||||
|
imagePaths,
|
||||||
|
outputPath: path.join(process.cwd(), 'storage', 'pdf-output', `images-to-pdf-${uuidv4()}.pdf`),
|
||||||
|
fitToPage,
|
||||||
|
pageSize,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Calcula tamanho do arquivo resultante
|
||||||
|
const stats = await fs.stat(outputPath);
|
||||||
|
const tamanhoResultado = BigInt(stats.size);
|
||||||
|
|
||||||
|
const tempoProcessamento = Date.now() - startTime;
|
||||||
|
|
||||||
|
// Atualiza a operação
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'CONCLUIDO',
|
||||||
|
arquivosResultado: [outputPath],
|
||||||
|
tamanhoResultado,
|
||||||
|
tempoProcessamento,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove os arquivos originais
|
||||||
|
for (const imagePath of imagePaths) {
|
||||||
|
await fs.unlink(imagePath).catch(() => {});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
operacaoId,
|
||||||
|
outputPath,
|
||||||
|
imagesProcessed: files.length,
|
||||||
|
pdfSize: Number(tamanhoResultado),
|
||||||
|
processingTime: tempoProcessamento,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao converter imagens para PDF:', error);
|
||||||
|
|
||||||
|
if (operacaoId) {
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'ERRO',
|
||||||
|
mensagemErro: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
tempoProcessamento: Date.now() - startTime,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao converter imagens para PDF',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Realiza OCR em PDF
|
||||||
|
*/
|
||||||
|
static async performOCR(req: Request, res: Response): Promise<void> {
|
||||||
|
const startTime = Date.now();
|
||||||
|
let operacaoId: string | null = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const file = (req.files as Express.Multer.File[])?.[0];
|
||||||
|
const userId = (req as any).user?.id;
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
res.status(400).json({ error: 'Nenhum arquivo enviado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const language = req.body.language || 'por';
|
||||||
|
const pages = req.body.pages ? JSON.parse(req.body.pages) : 'all';
|
||||||
|
|
||||||
|
// Cria registro da operação
|
||||||
|
const operacao = await prisma.operacaoPDF.create({
|
||||||
|
data: {
|
||||||
|
tipoOperacao: 'OCR',
|
||||||
|
status: 'PROCESSANDO',
|
||||||
|
usuarioId: userId,
|
||||||
|
arquivosOriginais: [file.path],
|
||||||
|
arquivosResultado: [],
|
||||||
|
tamanhoOriginal: BigInt(file.size),
|
||||||
|
parametros: { language, pages },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
operacaoId = operacao.id;
|
||||||
|
|
||||||
|
// Realiza OCR
|
||||||
|
const ocrResult = await PDFService.performOCR(file.path, {
|
||||||
|
language,
|
||||||
|
pages,
|
||||||
|
});
|
||||||
|
|
||||||
|
const tempoProcessamento = Date.now() - startTime;
|
||||||
|
|
||||||
|
// Atualiza a operação
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'CONCLUIDO',
|
||||||
|
detalhesOCR: ocrResult as any,
|
||||||
|
tempoProcessamento,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove o arquivo original
|
||||||
|
await fs.unlink(file.path).catch(() => {});
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
operacaoId,
|
||||||
|
result: ocrResult,
|
||||||
|
processingTime: tempoProcessamento,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao realizar OCR:', error);
|
||||||
|
|
||||||
|
if (operacaoId) {
|
||||||
|
await prisma.operacaoPDF.update({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
data: {
|
||||||
|
status: 'ERRO',
|
||||||
|
mensagemErro: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
tempoProcessamento: Date.now() - startTime,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao realizar OCR',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtém informações sobre um PDF
|
||||||
|
*/
|
||||||
|
static async getInfo(req: Request, res: Response): Promise<void> {
|
||||||
|
try {
|
||||||
|
const file = (req.files as Express.Multer.File[])?.[0];
|
||||||
|
|
||||||
|
if (!file) {
|
||||||
|
res.status(400).json({ error: 'Nenhum arquivo enviado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const info = await PDFService.getPDFInfo(file.path);
|
||||||
|
|
||||||
|
// Remove o arquivo
|
||||||
|
await fs.unlink(file.path).catch(() => {});
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
info,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao obter informações do PDF:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao obter informações do PDF',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtém histórico de operações do usuário
|
||||||
|
*/
|
||||||
|
static async getHistory(req: Request, res: Response): Promise<void> {
|
||||||
|
try {
|
||||||
|
const userId = (req as any).user?.id;
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const page = parseInt(req.query.page as string) || 1;
|
||||||
|
const limit = parseInt(req.query.limit as string) || 20;
|
||||||
|
const skip = (page - 1) * limit;
|
||||||
|
|
||||||
|
const [operacoes, total] = await Promise.all([
|
||||||
|
prisma.operacaoPDF.findMany({
|
||||||
|
where: { usuarioId: userId },
|
||||||
|
orderBy: { criadoEm: 'desc' },
|
||||||
|
skip,
|
||||||
|
take: limit,
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
tipoOperacao: true,
|
||||||
|
status: true,
|
||||||
|
tamanhoOriginal: true,
|
||||||
|
tamanhoResultado: true,
|
||||||
|
tempoProcessamento: true,
|
||||||
|
mensagemErro: true,
|
||||||
|
criadoEm: true,
|
||||||
|
parametros: true,
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
prisma.operacaoPDF.count({
|
||||||
|
where: { usuarioId: userId },
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
operacoes,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
limit,
|
||||||
|
total,
|
||||||
|
pages: Math.ceil(total / limit),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao obter histórico:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao obter histórico',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Download de arquivo processado
|
||||||
|
*/
|
||||||
|
static async downloadFile(req: Request, res: Response): Promise<void> {
|
||||||
|
try {
|
||||||
|
const { operacaoId, fileIndex } = req.params;
|
||||||
|
const userId = (req as any).user?.id;
|
||||||
|
|
||||||
|
if (!userId) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const operacao = await prisma.operacaoPDF.findUnique({
|
||||||
|
where: { id: operacaoId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!operacao) {
|
||||||
|
res.status(404).json({ error: 'Operação não encontrada' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (operacao.usuarioId !== userId) {
|
||||||
|
res.status(403).json({ error: 'Acesso negado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const index = parseInt(fileIndex);
|
||||||
|
const filePath = operacao.arquivosResultado[index];
|
||||||
|
|
||||||
|
if (!filePath) {
|
||||||
|
res.status(404).json({ error: 'Arquivo não encontrado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.download(filePath, path.basename(filePath));
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao fazer download:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Falha ao fazer download',
|
||||||
|
message: error instanceof Error ? error.message : 'Erro desconhecido',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
import { Request, Response, NextFunction } from 'express';
|
||||||
|
import jwt from 'jsonwebtoken';
|
||||||
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
// Adicionar interface para tipo de usuário no Request
|
||||||
|
declare global {
|
||||||
|
namespace Express {
|
||||||
|
interface Request {
|
||||||
|
user?: {
|
||||||
|
id: string;
|
||||||
|
nome: string;
|
||||||
|
email: string;
|
||||||
|
perfil: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const authenticateToken = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
// Verificar se há token
|
||||||
|
const authHeader = req.headers.authorization;
|
||||||
|
if (!authHeader) {
|
||||||
|
res.status(401).json({ error: 'Token não fornecido' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extrair o token do header (remover o Bearer)
|
||||||
|
const token = authHeader.replace('Bearer ', '');
|
||||||
|
|
||||||
|
// Verificar o token
|
||||||
|
const secret = process.env.JWT_SECRET || 'default-secret';
|
||||||
|
const decoded = jwt.verify(token, secret) as any;
|
||||||
|
|
||||||
|
// Buscar o usuário no banco
|
||||||
|
const user = await prisma.usuario.findUnique({
|
||||||
|
where: { id: decoded.id },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
nome: true,
|
||||||
|
email: true,
|
||||||
|
perfil: true,
|
||||||
|
ativo: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!user || !user.ativo) {
|
||||||
|
res.status(401).json({ error: 'Usuário inválido ou inativo' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adicionar o usuário no request
|
||||||
|
req.user = {
|
||||||
|
id: user.id,
|
||||||
|
nome: user.nome,
|
||||||
|
email: user.email,
|
||||||
|
perfil: user.perfil,
|
||||||
|
};
|
||||||
|
|
||||||
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro na autenticação:', error);
|
||||||
|
|
||||||
|
if (error instanceof jwt.TokenExpiredError) {
|
||||||
|
res.status(401).json({ error: 'Token expirado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error instanceof jwt.JsonWebTokenError) {
|
||||||
|
res.status(401).json({ error: 'Token inválido' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(500).json({ error: 'Erro ao autenticar' });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const requireAdmin = async (
|
||||||
|
req: Request,
|
||||||
|
res: Response,
|
||||||
|
next: NextFunction
|
||||||
|
): Promise<void> => {
|
||||||
|
try {
|
||||||
|
if (!req.user) {
|
||||||
|
res.status(401).json({ error: 'Usuário não autenticado' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.user.perfil !== 'ADVOGADO_ADMIN') {
|
||||||
|
res.status(403).json({ error: 'Acesso negado. Apenas administradores.' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro na verificação de permissão:', error);
|
||||||
|
res.status(500).json({ error: 'Erro ao verificar permissões' });
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import { PDFController, uploadMiddleware } from '../controllers/pdfController';
|
||||||
|
import { authenticateToken } from '../middleware/auth';
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rotas para manipulação de PDF
|
||||||
|
* Todas as rotas requerem autenticação
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Comprime PDFs
|
||||||
|
router.post(
|
||||||
|
'/compress',
|
||||||
|
authenticateToken,
|
||||||
|
uploadMiddleware.array('files', 10),
|
||||||
|
PDFController.compress
|
||||||
|
);
|
||||||
|
|
||||||
|
// Divide PDF
|
||||||
|
router.post(
|
||||||
|
'/split',
|
||||||
|
authenticateToken,
|
||||||
|
uploadMiddleware.array('files', 1),
|
||||||
|
PDFController.split
|
||||||
|
);
|
||||||
|
|
||||||
|
// Mescla PDFs
|
||||||
|
router.post(
|
||||||
|
'/merge',
|
||||||
|
authenticateToken,
|
||||||
|
uploadMiddleware.array('files', 50),
|
||||||
|
PDFController.merge
|
||||||
|
);
|
||||||
|
|
||||||
|
// Converte PDF para imagens
|
||||||
|
router.post(
|
||||||
|
'/pdf-to-images',
|
||||||
|
authenticateToken,
|
||||||
|
uploadMiddleware.array('files', 1),
|
||||||
|
PDFController.pdfToImages
|
||||||
|
);
|
||||||
|
|
||||||
|
// Converte imagens para PDF
|
||||||
|
router.post(
|
||||||
|
'/images-to-pdf',
|
||||||
|
authenticateToken,
|
||||||
|
uploadMiddleware.array('files', 100),
|
||||||
|
PDFController.imagesToPDF
|
||||||
|
);
|
||||||
|
|
||||||
|
// Realiza OCR em PDF
|
||||||
|
router.post(
|
||||||
|
'/ocr',
|
||||||
|
authenticateToken,
|
||||||
|
uploadMiddleware.array('files', 1),
|
||||||
|
PDFController.performOCR
|
||||||
|
);
|
||||||
|
|
||||||
|
// Obtém informações de um PDF
|
||||||
|
router.post(
|
||||||
|
'/info',
|
||||||
|
authenticateToken,
|
||||||
|
uploadMiddleware.array('files', 1),
|
||||||
|
PDFController.getInfo
|
||||||
|
);
|
||||||
|
|
||||||
|
// Obtém histórico de operações
|
||||||
|
router.get(
|
||||||
|
'/history',
|
||||||
|
authenticateToken,
|
||||||
|
PDFController.getHistory
|
||||||
|
);
|
||||||
|
|
||||||
|
// Download de arquivo processado
|
||||||
|
router.get(
|
||||||
|
'/download/:operacaoId/:fileIndex',
|
||||||
|
authenticateToken,
|
||||||
|
PDFController.downloadFile
|
||||||
|
);
|
||||||
|
|
||||||
|
export default router;
|
||||||
@@ -0,0 +1,421 @@
|
|||||||
|
# Funcionalidade de Manipulação de PDFs
|
||||||
|
|
||||||
|
## Visão Geral
|
||||||
|
|
||||||
|
Sistema completo de manipulação de arquivos PDF com as seguintes funcionalidades:
|
||||||
|
|
||||||
|
### Recursos Implementados
|
||||||
|
|
||||||
|
1. **Compressão de PDF sem perda de qualidade**
|
||||||
|
- Compressão inteligente mantendo qualidade entre 50-100%
|
||||||
|
- Remoção de metadados desnecessários
|
||||||
|
- Otimização de streams de objetos
|
||||||
|
- Suporte para múltiplos arquivos simultâneos
|
||||||
|
|
||||||
|
2. **Divisão de PDF**
|
||||||
|
- Por páginas específicas (ex: 1,3,5,7)
|
||||||
|
- Por intervalos (ex: 1-5, 6-10)
|
||||||
|
- Por chunks de tamanho fixo
|
||||||
|
|
||||||
|
3. **Mesclagem de PDFs**
|
||||||
|
- Combine até 50 PDFs em um único arquivo
|
||||||
|
- Preserva qualidade original
|
||||||
|
- Ordem personalizável
|
||||||
|
|
||||||
|
4. **PDF para Imagens**
|
||||||
|
- Formatos: PNG, JPG, WEBP
|
||||||
|
- DPI ajustável (150-600)
|
||||||
|
- Qualidade configurável (50-100%)
|
||||||
|
- Conversão de páginas específicas ou todas
|
||||||
|
|
||||||
|
5. **Imagens para PDF**
|
||||||
|
- Suporta múltiplas imagens (até 100)
|
||||||
|
- Ajuste automático ao tamanho da página
|
||||||
|
- Suporta PNG, JPG, JPEG, WEBP
|
||||||
|
|
||||||
|
6. **OCR (Reconhecimento Ótico de Caracteres)**
|
||||||
|
- Extração de texto de PDFs escaneados
|
||||||
|
- Suporte para Português, Inglês e Espanhol
|
||||||
|
- Retorna confiança da extração
|
||||||
|
- Processamento por página
|
||||||
|
|
||||||
|
7. **Informações do PDF**
|
||||||
|
- Número de páginas
|
||||||
|
- Tamanho do arquivo
|
||||||
|
- Metadados (título, autor, criador, etc.)
|
||||||
|
- Datas de criação e modificação
|
||||||
|
|
||||||
|
## Instalação
|
||||||
|
|
||||||
|
### 1. Instalar Dependências
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install pdf-lib pdf-parse pdf2pic tesseract.js
|
||||||
|
npm install --save-dev @types/pdf-parse
|
||||||
|
```
|
||||||
|
|
||||||
|
Ou execute o script de instalação:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run install-pdf-deps
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Executar Migration do Banco de Dados
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run prisma:migrate
|
||||||
|
```
|
||||||
|
|
||||||
|
Ou manualmente:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npx prisma migrate dev --name add_operacoes_pdf
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Gerar Cliente Prisma
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run prisma:generate
|
||||||
|
```
|
||||||
|
|
||||||
|
### 4. Criar Diretórios Necessários
|
||||||
|
|
||||||
|
Os diretórios serão criados automaticamente na primeira execução, mas você pode criá-los manualmente:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mkdir -p storage/temp/pdf-ops
|
||||||
|
mkdir -p storage/pdf-output
|
||||||
|
mkdir -p storage/temp/uploads
|
||||||
|
```
|
||||||
|
|
||||||
|
## Uso
|
||||||
|
|
||||||
|
### Frontend
|
||||||
|
|
||||||
|
Acesse a página de manipulação de PDFs através da rota:
|
||||||
|
|
||||||
|
```
|
||||||
|
/pdf
|
||||||
|
```
|
||||||
|
|
||||||
|
### API Endpoints
|
||||||
|
|
||||||
|
Todas as rotas requerem autenticação via Bearer token.
|
||||||
|
|
||||||
|
#### 1. Comprimir PDF
|
||||||
|
|
||||||
|
```http
|
||||||
|
POST /api/pdf/compress
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
|
||||||
|
Body:
|
||||||
|
- files: File[] (array de PDFs)
|
||||||
|
- quality: number (50-100, padrão 95)
|
||||||
|
- maxSizeMB: number (opcional)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 2. Dividir PDF
|
||||||
|
|
||||||
|
```http
|
||||||
|
POST /api/pdf/split
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
|
||||||
|
Body:
|
||||||
|
- files: File (único PDF)
|
||||||
|
- splitType: 'pages' | 'range' | 'chunks'
|
||||||
|
- pages: string (ex: "1,2,3") - para splitType='pages'
|
||||||
|
- ranges: string (ex: "1-5,6-10") - para splitType='range'
|
||||||
|
- chunkSize: number - para splitType='chunks'
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 3. Mesclar PDFs
|
||||||
|
|
||||||
|
```http
|
||||||
|
POST /api/pdf/merge
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
|
||||||
|
Body:
|
||||||
|
- files: File[] (mínimo 2 PDFs)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 4. PDF para Imagens
|
||||||
|
|
||||||
|
```http
|
||||||
|
POST /api/pdf/pdf-to-images
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
|
||||||
|
Body:
|
||||||
|
- files: File (único PDF)
|
||||||
|
- format: 'png' | 'jpg' | 'webp' (padrão: png)
|
||||||
|
- quality: number (50-100, padrão: 95)
|
||||||
|
- density: number (DPI, 150-600, padrão: 300)
|
||||||
|
- pages: string (opcional, ex: "[1,2,3]" ou "all")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 5. Imagens para PDF
|
||||||
|
|
||||||
|
```http
|
||||||
|
POST /api/pdf/images-to-pdf
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
|
||||||
|
Body:
|
||||||
|
- files: File[] (array de imagens)
|
||||||
|
- fitToPage: boolean (padrão: true)
|
||||||
|
- pageSize: string (opcional, JSON: {"width":595,"height":842})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 6. OCR
|
||||||
|
|
||||||
|
```http
|
||||||
|
POST /api/pdf/ocr
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
|
||||||
|
Body:
|
||||||
|
- files: File (único PDF)
|
||||||
|
- language: string ('por' | 'eng' | 'spa', padrão: 'por')
|
||||||
|
- pages: string (opcional, ex: "[1,2]" ou "all")
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 7. Informações do PDF
|
||||||
|
|
||||||
|
```http
|
||||||
|
POST /api/pdf/info
|
||||||
|
Content-Type: multipart/form-data
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
|
||||||
|
Body:
|
||||||
|
- files: File (único PDF)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 8. Histórico de Operações
|
||||||
|
|
||||||
|
```http
|
||||||
|
GET /api/pdf/history?page=1&limit=20
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 9. Download de Arquivo Processado
|
||||||
|
|
||||||
|
```http
|
||||||
|
GET /api/pdf/download/:operacaoId/:fileIndex
|
||||||
|
Authorization: Bearer {token}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Estrutura de Resposta
|
||||||
|
|
||||||
|
### Sucesso (Compressão)
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"operacaoId": "uuid",
|
||||||
|
"results": [
|
||||||
|
{
|
||||||
|
"originalName": "documento.pdf",
|
||||||
|
"originalSize": 1048576,
|
||||||
|
"compressedSize": 524288,
|
||||||
|
"reduction": 50.0,
|
||||||
|
"outputPath": "/path/to/compressed.pdf"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"totalOriginalSize": 1048576,
|
||||||
|
"totalCompressedSize": 524288,
|
||||||
|
"totalReduction": 50.0,
|
||||||
|
"processingTime": 1234
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sucesso (OCR)
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"success": true,
|
||||||
|
"operacaoId": "uuid",
|
||||||
|
"result": {
|
||||||
|
"text": "Texto extraído completo...",
|
||||||
|
"confidence": 95.5,
|
||||||
|
"pages": [
|
||||||
|
{
|
||||||
|
"pageNumber": 1,
|
||||||
|
"text": "Texto da página 1...",
|
||||||
|
"confidence": 96.2
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"processingTime": 5678
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Erro
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"error": "Falha ao processar arquivo",
|
||||||
|
"message": "Detalhes específicos do erro"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Modelo de Dados
|
||||||
|
|
||||||
|
### OperacaoPDF
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface OperacaoPDF {
|
||||||
|
id: string;
|
||||||
|
tipoOperacao: 'COMPRIMIR' | 'DIVIDIR' | 'MESCLAR' | 'PDF_PARA_IMAGEM' | 'IMAGEM_PARA_PDF' | 'OCR';
|
||||||
|
status: 'PROCESSANDO' | 'CONCLUIDO' | 'ERRO';
|
||||||
|
usuarioId: string;
|
||||||
|
arquivosOriginais: string[];
|
||||||
|
arquivosResultado: string[];
|
||||||
|
tamanhoOriginal: bigint;
|
||||||
|
tamanhoResultado: bigint | null;
|
||||||
|
parametros: any;
|
||||||
|
mensagemErro: string | null;
|
||||||
|
detalhesOCR: any | null;
|
||||||
|
tempoProcessamento: number | null;
|
||||||
|
criadoEm: Date;
|
||||||
|
atualizadoEm: Date;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Segurança
|
||||||
|
|
||||||
|
- ✅ Autenticação obrigatória em todas as rotas
|
||||||
|
- ✅ Validação de tipos de arquivo (apenas PDF e imagens)
|
||||||
|
- ✅ Limite de tamanho de arquivo (100MB por arquivo)
|
||||||
|
- ✅ Limite de quantidade de arquivos por operação
|
||||||
|
- ✅ Sanitização de nomes de arquivo
|
||||||
|
- ✅ Isolamento de arquivos por operação
|
||||||
|
- ✅ Limpeza automática de arquivos temporários
|
||||||
|
|
||||||
|
## Performance
|
||||||
|
|
||||||
|
- ✅ Processamento assíncrono
|
||||||
|
- ✅ Otimização de memória com streams
|
||||||
|
- ✅ Compressão sem perda de qualidade perceptível
|
||||||
|
- ✅ Cache de workers de OCR
|
||||||
|
- ✅ Índices no banco de dados para consultas rápidas
|
||||||
|
- ✅ Limpeza automática de arquivos antigos
|
||||||
|
|
||||||
|
## Tratamento de Erros
|
||||||
|
|
||||||
|
Todos os erros são:
|
||||||
|
- Logados no console do servidor
|
||||||
|
- Registrados no banco de dados (tabela operacoes_pdf)
|
||||||
|
- Retornados ao cliente com mensagens descritivas
|
||||||
|
- Capturados por try-catch em todas as operações
|
||||||
|
|
||||||
|
## Limitações
|
||||||
|
|
||||||
|
- Tamanho máximo por arquivo: 100MB
|
||||||
|
- Máximo de arquivos para compressão: 10
|
||||||
|
- Máximo de arquivos para mesclagem: 50
|
||||||
|
- Máximo de imagens para conversão em PDF: 100
|
||||||
|
- Arquivos processados são mantidos por 24 horas
|
||||||
|
|
||||||
|
## Manutenção
|
||||||
|
|
||||||
|
### Limpeza de Arquivos Antigos
|
||||||
|
|
||||||
|
Execute periodicamente (recomendado: diariamente via cron):
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { PDFService } from './src/services/pdfService';
|
||||||
|
|
||||||
|
// Remove arquivos com mais de 24 horas
|
||||||
|
await PDFService.cleanupOldFiles(24);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Monitoramento
|
||||||
|
|
||||||
|
Consulte a tabela `operacoes_pdf` para:
|
||||||
|
- Estatísticas de uso
|
||||||
|
- Taxa de erro
|
||||||
|
- Tempo médio de processamento
|
||||||
|
- Operações mais utilizadas
|
||||||
|
|
||||||
|
```sql
|
||||||
|
-- Operações por tipo
|
||||||
|
SELECT "tipoOperacao", COUNT(*) as total
|
||||||
|
FROM operacoes_pdf
|
||||||
|
GROUP BY "tipoOperacao"
|
||||||
|
ORDER BY total DESC;
|
||||||
|
|
||||||
|
-- Taxa de sucesso
|
||||||
|
SELECT
|
||||||
|
status,
|
||||||
|
COUNT(*) as total,
|
||||||
|
ROUND(COUNT(*) * 100.0 / SUM(COUNT(*)) OVER(), 2) as percentual
|
||||||
|
FROM operacoes_pdf
|
||||||
|
GROUP BY status;
|
||||||
|
|
||||||
|
-- Tempo médio por operação
|
||||||
|
SELECT
|
||||||
|
"tipoOperacao",
|
||||||
|
AVG("tempoProcessamento") as tempo_medio_ms,
|
||||||
|
MIN("tempoProcessamento") as tempo_min_ms,
|
||||||
|
MAX("tempoProcessamento") as tempo_max_ms
|
||||||
|
FROM operacoes_pdf
|
||||||
|
WHERE status = 'CONCLUIDO'
|
||||||
|
GROUP BY "tipoOperacao";
|
||||||
|
```
|
||||||
|
|
||||||
|
## Troubleshooting
|
||||||
|
|
||||||
|
### Erro: "Tesseract worker failed"
|
||||||
|
|
||||||
|
Solução: Certifique-se de que o Tesseract está instalado corretamente:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install tesseract.js --force
|
||||||
|
```
|
||||||
|
|
||||||
|
### Erro: "Cannot find module 'sharp'"
|
||||||
|
|
||||||
|
Solução: Reinstale o Sharp com rebuild:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm uninstall sharp
|
||||||
|
npm install sharp
|
||||||
|
npm rebuild sharp
|
||||||
|
```
|
||||||
|
|
||||||
|
### Erro: "ENOSPC: no space left on device"
|
||||||
|
|
||||||
|
Solução: Aumente o espaço em disco ou execute a limpeza:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run cleanup-pdf-files
|
||||||
|
```
|
||||||
|
|
||||||
|
### Performance lenta no OCR
|
||||||
|
|
||||||
|
Solução: Reduza o DPI ou processe menos páginas por vez.
|
||||||
|
|
||||||
|
## Suporte
|
||||||
|
|
||||||
|
Para questões ou problemas:
|
||||||
|
1. Verifique os logs do servidor
|
||||||
|
2. Consulte a tabela `operacoes_pdf` para detalhes do erro
|
||||||
|
3. Verifique se os diretórios de storage têm permissões corretas
|
||||||
|
4. Certifique-se de que todas as dependências estão instaladas
|
||||||
|
|
||||||
|
## Changelog
|
||||||
|
|
||||||
|
### v1.0.0 (2026-02-27)
|
||||||
|
- ✅ Implementação inicial
|
||||||
|
- ✅ Compressão sem perda de qualidade
|
||||||
|
- ✅ Divisão de PDFs
|
||||||
|
- ✅ Mesclagem de PDFs
|
||||||
|
- ✅ Conversão PDF para imagens
|
||||||
|
- ✅ Conversão de imagens para PDF
|
||||||
|
- ✅ OCR com Tesseract
|
||||||
|
- ✅ Histórico de operações
|
||||||
|
- ✅ Interface React completa
|
||||||
|
- ✅ Validação e segurança
|
||||||
+7
-1
@@ -19,7 +19,9 @@
|
|||||||
"lint:fix": "eslint . --ext .ts,.tsx --fix",
|
"lint:fix": "eslint . --ext .ts,.tsx --fix",
|
||||||
"create-admin": "ts-node scripts/create_admin_user.ts",
|
"create-admin": "ts-node scripts/create_admin_user.ts",
|
||||||
"worker": "ts-node src/workers/startWorker.ts",
|
"worker": "ts-node src/workers/startWorker.ts",
|
||||||
"install-deps": "npm install ua-parser-js uuid axios xlsx"
|
"install-deps": "npm install ua-parser-js uuid axios xlsx",
|
||||||
|
"install-pdf-deps": "npm install pdf-parse pdf2pic tesseract.js && npm install --save-dev @types/pdf-parse",
|
||||||
|
"cleanup-pdf-files": "ts-node scripts/cleanup_pdf_files.ts"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@emotion/react": "^11.11.3",
|
"@emotion/react": "^11.11.3",
|
||||||
@@ -49,9 +51,12 @@
|
|||||||
"multer": "^1.4.5-lts.1",
|
"multer": "^1.4.5-lts.1",
|
||||||
"opentimestamps-javascript": "^1.0.1",
|
"opentimestamps-javascript": "^1.0.1",
|
||||||
"pdf-lib": "^1.17.1",
|
"pdf-lib": "^1.17.1",
|
||||||
|
"pdf-parse": "^1.1.1",
|
||||||
|
"pdf2pic": "^3.1.3",
|
||||||
"pdfkit": "^0.13.0",
|
"pdfkit": "^0.13.0",
|
||||||
"puppeteer": "^24.5.0",
|
"puppeteer": "^24.5.0",
|
||||||
"qrcode": "^1.5.3",
|
"qrcode": "^1.5.3",
|
||||||
|
"tesseract.js": "^5.0.4",
|
||||||
"react": "^18.2.0",
|
"react": "^18.2.0",
|
||||||
"react-beautiful-dnd": "^13.1.1",
|
"react-beautiful-dnd": "^13.1.1",
|
||||||
"react-dom": "^18.2.0",
|
"react-dom": "^18.2.0",
|
||||||
@@ -77,6 +82,7 @@
|
|||||||
"@types/jsonwebtoken": "^9.0.9",
|
"@types/jsonwebtoken": "^9.0.9",
|
||||||
"@types/multer": "^1.4.12",
|
"@types/multer": "^1.4.12",
|
||||||
"@types/node": "^22.13.14",
|
"@types/node": "^22.13.14",
|
||||||
|
"@types/pdf-parse": "^1.1.4",
|
||||||
"@types/react": "^19.0.12",
|
"@types/react": "^19.0.12",
|
||||||
"@types/react-beautiful-dnd": "^13.1.8",
|
"@types/react-beautiful-dnd": "^13.1.8",
|
||||||
"@types/react-dom": "^19.0.4",
|
"@types/react-dom": "^19.0.4",
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
-- Migration para adicionar tabela de operações de PDF
|
||||||
|
-- Created: 2026-02-27
|
||||||
|
|
||||||
|
-- Criar ENUM para tipo de operação
|
||||||
|
CREATE TYPE "TipoOperacaoPDF" AS ENUM ('COMPRIMIR', 'DIVIDIR', 'MESCLAR', 'PDF_PARA_IMAGEM', 'IMAGEM_PARA_PDF', 'OCR');
|
||||||
|
|
||||||
|
-- Criar ENUM para status da operação
|
||||||
|
CREATE TYPE "StatusOperacaoPDF" AS ENUM ('PROCESSANDO', 'CONCLUIDO', 'ERRO');
|
||||||
|
|
||||||
|
-- Criar tabela de operações de PDF
|
||||||
|
CREATE TABLE "operacoes_pdf" (
|
||||||
|
"id" UUID NOT NULL DEFAULT gen_random_uuid(),
|
||||||
|
"tipoOperacao" "TipoOperacaoPDF" NOT NULL,
|
||||||
|
"status" "StatusOperacaoPDF" NOT NULL DEFAULT 'PROCESSANDO',
|
||||||
|
"usuarioId" UUID NOT NULL,
|
||||||
|
"arquivosOriginais" TEXT[] NOT NULL,
|
||||||
|
"arquivosResultado" TEXT[] NOT NULL,
|
||||||
|
"tamanhoOriginal" BIGINT NOT NULL,
|
||||||
|
"tamanhoResultado" BIGINT,
|
||||||
|
"parametros" JSONB,
|
||||||
|
"mensagemErro" TEXT,
|
||||||
|
"detalhesOCR" JSONB,
|
||||||
|
"tempoProcessamento" INTEGER,
|
||||||
|
"criadoEm" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"atualizadoEm" TIMESTAMP(3) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "operacoes_pdf_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Adicionar foreign key para usuariosALTER TABLE "operacoes_pdf" ADD CONSTRAINT "operacoes_pdf_usuarioId_fkey" FOREIGN KEY ("usuarioId") REFERENCES "usuarios"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- Criar índice para melhor performance nas consultas
|
||||||
|
CREATE INDEX "operacoes_pdf_usuarioId_idx" ON "operacoes_pdf"("usuarioId");
|
||||||
|
CREATE INDEX "operacoes_pdf_criadoEm_idx" ON "operacoes_pdf"("criadoEm" DESC);
|
||||||
|
CREATE INDEX "operacoes_pdf_status_idx" ON "operacoes_pdf"("status");
|
||||||
@@ -51,6 +51,9 @@ model Usuario {
|
|||||||
// Adicionar relação para cálculos de pensão alimentícia
|
// Adicionar relação para cálculos de pensão alimentícia
|
||||||
calculosPensaoAlimenticia CalculoPensaoAlimenticia[]
|
calculosPensaoAlimenticia CalculoPensaoAlimenticia[]
|
||||||
|
|
||||||
|
// Adicionar relação para operações de PDF
|
||||||
|
operacoesPDF OperacaoPDF[]
|
||||||
|
|
||||||
@@map("usuarios")
|
@@map("usuarios")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1067,3 +1070,47 @@ model CalculoPensaoAlimenticia {
|
|||||||
|
|
||||||
@@map("calculos_pensao_alimenticia")
|
@@map("calculos_pensao_alimenticia")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Modelo para histórico de operações de PDF
|
||||||
|
enum TipoOperacaoPDF {
|
||||||
|
COMPRIMIR
|
||||||
|
DIVIDIR
|
||||||
|
MESCLAR
|
||||||
|
PDF_PARA_IMAGEM
|
||||||
|
IMAGEM_PARA_PDF
|
||||||
|
OCR
|
||||||
|
}
|
||||||
|
|
||||||
|
enum StatusOperacaoPDF {
|
||||||
|
PROCESSANDO
|
||||||
|
CONCLUIDO
|
||||||
|
ERRO
|
||||||
|
}
|
||||||
|
|
||||||
|
model OperacaoPDF {
|
||||||
|
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
||||||
|
tipoOperacao TipoOperacaoPDF
|
||||||
|
status StatusOperacaoPDF @default(PROCESSANDO)
|
||||||
|
usuarioId String @db.Uuid
|
||||||
|
usuario Usuario @relation(fields: [usuarioId], references: [id])
|
||||||
|
|
||||||
|
// Informações dos arquivos
|
||||||
|
arquivosOriginais String[] // Array de paths dos arquivos originais
|
||||||
|
arquivosResultado String[] // Array de paths dos arquivos resultantes
|
||||||
|
tamanhoOriginal BigInt // Tamanho total dos arquivos originais em bytes
|
||||||
|
tamanhoResultado BigInt? // Tamanho total dos arquivos resultantes em bytes
|
||||||
|
|
||||||
|
// Parâmetros da operação
|
||||||
|
parametros Json? // Parâmetros específicos da operação (ex: páginas para dividir)
|
||||||
|
|
||||||
|
// Resultado da operação
|
||||||
|
mensagemErro String?
|
||||||
|
detalhesOCR Json? // Quando for OCR, armazena o texto extraído
|
||||||
|
|
||||||
|
tempoProcessamento Int? // Tempo de processamento em milissegundos
|
||||||
|
|
||||||
|
criadoEm DateTime @default(now())
|
||||||
|
atualizadoEm DateTime @updatedAt
|
||||||
|
|
||||||
|
@@map("operacoes_pdf")
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { PDFService } from '../src/services/pdfService';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script para limpeza de arquivos PDF antigos
|
||||||
|
* Remove arquivos com mais de 24 horas por padrão
|
||||||
|
*
|
||||||
|
* Uso:
|
||||||
|
* npm run cleanup-pdf-files
|
||||||
|
*
|
||||||
|
* Ou via ts-node:
|
||||||
|
* ts-node scripts/cleanup_pdf_files.ts [horas]
|
||||||
|
*/
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
console.log('Iniciando limpeza de arquivos PDF antigos...');
|
||||||
|
|
||||||
|
const horasArg = process.argv[2];
|
||||||
|
const horas = horasArg ? parseInt(horasArg) : 24;
|
||||||
|
|
||||||
|
if (isNaN(horas) || horas < 0) {
|
||||||
|
console.error('Erro: O argumento deve ser um número positivo de horas');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Removendo arquivos com mais de ${horas} horas...`);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await PDFService.cleanupOldFiles(horas);
|
||||||
|
console.log('✅ Limpeza concluída com sucesso!');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Erro durante a limpeza:', error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
.then(() => process.exit(0))
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('Erro fatal:', error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
@@ -0,0 +1,271 @@
|
|||||||
|
import { PDFService } from '../src/services/pdfService';
|
||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs/promises';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Script de teste para funcionalidades de PDF
|
||||||
|
*
|
||||||
|
* ATENÇÃO: Este é um script de teste. Certifique-se de ter arquivos de teste
|
||||||
|
* disponíveis antes de executar.
|
||||||
|
*
|
||||||
|
* Uso:
|
||||||
|
* ts-node scripts/test_pdf_operations.ts
|
||||||
|
*/
|
||||||
|
|
||||||
|
async function testPDFInfo() {
|
||||||
|
console.log('\n=== Testando: Informações do PDF ===');
|
||||||
|
try {
|
||||||
|
const testPdfPath = path.join(process.cwd(), 'test', 'sample.pdf');
|
||||||
|
|
||||||
|
// Verifica se o arquivo existe
|
||||||
|
try {
|
||||||
|
await fs.access(testPdfPath);
|
||||||
|
} catch {
|
||||||
|
console.log('⚠️ Arquivo de teste não encontrado. Pulando teste.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const info = await PDFService.getPDFInfo(testPdfPath);
|
||||||
|
console.log('✅ Informações obtidas com sucesso:');
|
||||||
|
console.log(' Páginas:', info.pages);
|
||||||
|
console.log(' Tamanho:', (info.size / 1024).toFixed(2), 'KB');
|
||||||
|
console.log(' Título:', info.title || 'N/A');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Erro:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testCompression() {
|
||||||
|
console.log('\n=== Testando: Compressão de PDF ===');
|
||||||
|
try {
|
||||||
|
const testPdfPath = path.join(process.cwd(), 'test', 'sample.pdf');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(testPdfPath);
|
||||||
|
} catch {
|
||||||
|
console.log('⚠️ Arquivo de teste não encontrado. Pulando teste.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await PDFService.compressPDF(testPdfPath, undefined, {
|
||||||
|
quality: 95,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('✅ Compressão concluída:');
|
||||||
|
console.log(' Tamanho original:', (result.originalSize / 1024).toFixed(2), 'KB');
|
||||||
|
console.log(' Tamanho comprimido:', (result.compressedSize / 1024).toFixed(2), 'KB');
|
||||||
|
console.log(' Redução:', result.reduction.toFixed(2), '%');
|
||||||
|
console.log(' Arquivo salvo em:', result.outputPath);
|
||||||
|
|
||||||
|
// Limpa o arquivo de teste
|
||||||
|
await fs.unlink(result.outputPath);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Erro:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testSplit() {
|
||||||
|
console.log('\n=== Testando: Divisão de PDF ===');
|
||||||
|
try {
|
||||||
|
const testPdfPath = path.join(process.cwd(), 'test', 'sample.pdf');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(testPdfPath);
|
||||||
|
} catch {
|
||||||
|
console.log('⚠️ Arquivo de teste não encontrado. Pulando teste.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Primeiro, verifica quantas páginas o PDF tem
|
||||||
|
const info = await PDFService.getPDFInfo(testPdfPath);
|
||||||
|
|
||||||
|
if (info.pages < 2) {
|
||||||
|
console.log('⚠️ PDF tem menos de 2 páginas. Pulando teste de divisão.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const outputPaths = await PDFService.splitPDF(testPdfPath, {
|
||||||
|
splitType: 'chunks',
|
||||||
|
chunkSize: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('✅ Divisão concluída:');
|
||||||
|
console.log(' Arquivos criados:', outputPaths.length);
|
||||||
|
|
||||||
|
// Limpa os arquivos de teste
|
||||||
|
for (const outputPath of outputPaths) {
|
||||||
|
await fs.unlink(outputPath);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Erro:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testMerge() {
|
||||||
|
console.log('\n=== Testando: Mesclagem de PDFs ===');
|
||||||
|
try {
|
||||||
|
const testPdf1 = path.join(process.cwd(), 'test', 'sample1.pdf');
|
||||||
|
const testPdf2 = path.join(process.cwd(), 'test', 'sample2.pdf');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(testPdf1);
|
||||||
|
await fs.access(testPdf2);
|
||||||
|
} catch {
|
||||||
|
console.log('⚠️ Arquivos de teste não encontrados. Pulando teste.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const outputPath = await PDFService.mergePDFs({
|
||||||
|
filePaths: [testPdf1, testPdf2],
|
||||||
|
outputPath: path.join(process.cwd(), 'storage', 'pdf-output', 'test-merged.pdf'),
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('✅ Mesclagem concluída:');
|
||||||
|
console.log(' Arquivo salvo em:', outputPath);
|
||||||
|
|
||||||
|
// Limpa o arquivo de teste
|
||||||
|
await fs.unlink(outputPath);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Erro:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testImagesToPDF() {
|
||||||
|
console.log('\n=== Testando: Imagens para PDF ===');
|
||||||
|
try {
|
||||||
|
const testImage1 = path.join(process.cwd(), 'test', 'image1.png');
|
||||||
|
const testImage2 = path.join(process.cwd(), 'test', 'image2.png');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(testImage1);
|
||||||
|
} catch {
|
||||||
|
console.log('⚠️ Imagens de teste não encontradas. Pulando teste.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const imagePaths = [testImage1];
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(testImage2);
|
||||||
|
imagePaths.push(testImage2);
|
||||||
|
} catch {
|
||||||
|
// Ignora se a segunda imagem não existe
|
||||||
|
}
|
||||||
|
|
||||||
|
const outputPath = await PDFService.imagesToPDF({
|
||||||
|
imagePaths,
|
||||||
|
outputPath: path.join(process.cwd(), 'storage', 'pdf-output', 'test-images.pdf'),
|
||||||
|
fitToPage: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('✅ Conversão concluída:');
|
||||||
|
console.log(' Imagens processadas:', imagePaths.length);
|
||||||
|
console.log(' Arquivo salvo em:', outputPath);
|
||||||
|
|
||||||
|
// Limpa o arquivo de teste
|
||||||
|
await fs.unlink(outputPath);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Erro:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testPDFToImages() {
|
||||||
|
console.log('\n=== Testando: PDF para Imagens ===');
|
||||||
|
try {
|
||||||
|
const testPdfPath = path.join(process.cwd(), 'test', 'sample.pdf');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(testPdfPath);
|
||||||
|
} catch {
|
||||||
|
console.log('⚠️ Arquivo de teste não encontrado. Pulando teste.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const outputPaths = await PDFService.pdfToImages(testPdfPath, {
|
||||||
|
format: 'png',
|
||||||
|
quality: 95,
|
||||||
|
density: 150, // Menor DPI para teste mais rápido
|
||||||
|
pages: [1], // Apenas primeira página
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('✅ Conversão concluída:');
|
||||||
|
console.log(' Imagens criadas:', outputPaths.length);
|
||||||
|
|
||||||
|
// Limpa os arquivos de teste
|
||||||
|
for (const outputPath of outputPaths) {
|
||||||
|
await fs.unlink(outputPath);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Erro:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function testOCR() {
|
||||||
|
console.log('\n=== Testando: OCR ===');
|
||||||
|
console.log('⚠️ OCR pode levar vários segundos. Aguarde...');
|
||||||
|
try {
|
||||||
|
const testPdfPath = path.join(process.cwd(), 'test', 'sample-text.pdf');
|
||||||
|
|
||||||
|
try {
|
||||||
|
await fs.access(testPdfPath);
|
||||||
|
} catch {
|
||||||
|
console.log('⚠️ Arquivo de teste não encontrado. Pulando teste.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await PDFService.performOCR(testPdfPath, {
|
||||||
|
language: 'por',
|
||||||
|
pages: [1], // Apenas primeira página
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('✅ OCR concluído:');
|
||||||
|
console.log(' Confiança:', result.confidence.toFixed(2), '%');
|
||||||
|
console.log(' Páginas processadas:', result.pages.length);
|
||||||
|
console.log(' Texto extraído (primeiros 100 caracteres):');
|
||||||
|
console.log(' ', result.text.substring(0, 100).replace(/\n/g, ' '));
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Erro:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
console.log('╔════════════════════════════════════════════╗');
|
||||||
|
console.log('║ Teste de Operações de PDF ║');
|
||||||
|
console.log('╚════════════════════════════════════════════╝');
|
||||||
|
|
||||||
|
console.log('\nPré-requisitos:');
|
||||||
|
console.log('- Coloque arquivos de teste na pasta "test/"');
|
||||||
|
console.log('- sample.pdf: PDF com múltiplas páginas');
|
||||||
|
console.log('- sample1.pdf e sample2.pdf: PDFs para mesclagem');
|
||||||
|
console.log('- image1.png e image2.png: Imagens para conversão');
|
||||||
|
console.log('- sample-text.pdf: PDF com texto para OCR');
|
||||||
|
|
||||||
|
// Criar diretório de teste se não existir
|
||||||
|
const testDir = path.join(process.cwd(), 'test');
|
||||||
|
try {
|
||||||
|
await fs.mkdir(testDir, { recursive: true });
|
||||||
|
} catch {
|
||||||
|
// Ignora se já existe
|
||||||
|
}
|
||||||
|
|
||||||
|
await PDFService.initialize();
|
||||||
|
|
||||||
|
await testPDFInfo();
|
||||||
|
await testCompression();
|
||||||
|
await testSplit();
|
||||||
|
await testMerge();
|
||||||
|
await testImagesToPDF();
|
||||||
|
await testPDFToImages();
|
||||||
|
await testOCR();
|
||||||
|
|
||||||
|
console.log('\n╔════════════════════════════════════════════╗');
|
||||||
|
console.log('║ Testes concluídos! ║');
|
||||||
|
console.log('╚════════════════════════════════════════════╝\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
.then(() => process.exit(0))
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('\n❌ Erro fatal durante os testes:', error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
+10
@@ -50,6 +50,9 @@ import { DetalhesCalculo } from './components/calculos/DetalhesCalculo';
|
|||||||
import { NovoPensaoAlimenticia } from './components/calculos/NovoPensaoAlimenticia';
|
import { NovoPensaoAlimenticia } from './components/calculos/NovoPensaoAlimenticia';
|
||||||
import { DetalhePensaoAlimenticia } from './components/calculos/DetalhePensaoAlimenticia';
|
import { DetalhePensaoAlimenticia } from './components/calculos/DetalhePensaoAlimenticia';
|
||||||
|
|
||||||
|
// Importar componente de manipulação de PDF
|
||||||
|
import { PDFManipulationPage } from './components/pdf/PDFManipulationPage';
|
||||||
|
|
||||||
const queryClient = new QueryClient();
|
const queryClient = new QueryClient();
|
||||||
|
|
||||||
const router = createBrowserRouter(
|
const router = createBrowserRouter(
|
||||||
@@ -126,6 +129,13 @@ const router = createBrowserRouter(
|
|||||||
<DetalhePensaoAlimenticia />
|
<DetalhePensaoAlimenticia />
|
||||||
</ProtectedRoute>
|
</ProtectedRoute>
|
||||||
} />
|
} />
|
||||||
|
|
||||||
|
{/* Rota para Manipulação de PDF */}
|
||||||
|
<Route path="/pdf" element={
|
||||||
|
<ProtectedRoute>
|
||||||
|
<PDFManipulationPage />
|
||||||
|
</ProtectedRoute>
|
||||||
|
} />
|
||||||
</Route>
|
</Route>
|
||||||
</Route>
|
</Route>
|
||||||
<Route path="*" element={<Navigate to="/login" />} />
|
<Route path="*" element={<Navigate to="/login" />} />
|
||||||
|
|||||||
@@ -0,0 +1,658 @@
|
|||||||
|
import React, { useState, useCallback } from 'react';
|
||||||
|
import { useDropzone } from 'react-dropzone';
|
||||||
|
import axios from 'axios';
|
||||||
|
import { toast } from 'react-toastify';
|
||||||
|
import {
|
||||||
|
DocumentIcon,
|
||||||
|
CloudArrowUpIcon,
|
||||||
|
ArrowsPointingInIcon,
|
||||||
|
ScissorsIcon,
|
||||||
|
ArrowsUpDownIcon,
|
||||||
|
PhotoIcon,
|
||||||
|
DocumentTextIcon,
|
||||||
|
InformationCircleIcon,
|
||||||
|
ArrowDownTrayIcon,
|
||||||
|
ClockIcon,
|
||||||
|
} from '@heroicons/react/24/outline';
|
||||||
|
|
||||||
|
interface OperationResult {
|
||||||
|
operacaoId: string;
|
||||||
|
outputPaths?: string[];
|
||||||
|
outputPath?: string;
|
||||||
|
processingTime?: number;
|
||||||
|
reduction?: number;
|
||||||
|
filesCreated?: number;
|
||||||
|
text?: string;
|
||||||
|
confidence?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
type OperationType =
|
||||||
|
| 'compress'
|
||||||
|
| 'split'
|
||||||
|
| 'merge'
|
||||||
|
| 'pdfToImages'
|
||||||
|
| 'imagesToPDF'
|
||||||
|
| 'ocr'
|
||||||
|
| 'info';
|
||||||
|
|
||||||
|
export const PDFManipulationPage: React.FC = () => {
|
||||||
|
const [selectedOperation, setSelectedOperation] = useState<OperationType>('compress');
|
||||||
|
const [files, setFiles] = useState<File[]>([]);
|
||||||
|
const [processing, setProcessing] = useState(false);
|
||||||
|
const [result, setResult] = useState<OperationResult | null>(null);
|
||||||
|
const [history, setHistory] = useState<any[]>([]);
|
||||||
|
|
||||||
|
// Configurações para cada operação
|
||||||
|
const [compressQuality, setCompressQuality] = useState(95);
|
||||||
|
const [splitType, setSplitType] = useState<'pages' | 'range' | 'chunks'>('pages');
|
||||||
|
const [splitPages, setSplitPages] = useState<string>('1,2,3');
|
||||||
|
const [splitRanges, setSplitRanges] = useState<string>('1-5,6-10');
|
||||||
|
const [chunkSize, setChunkSize] = useState(5);
|
||||||
|
const [imageFormat, setImageFormat] = useState<'png' | 'jpg' | 'webp'>('png');
|
||||||
|
const [imageQuality, setImageQuality] = useState(95);
|
||||||
|
const [imageDensity, setImageDensity] = useState(300);
|
||||||
|
const [ocrLanguage, setOcrLanguage] = useState('por');
|
||||||
|
|
||||||
|
const onDrop = useCallback((acceptedFiles: File[]) => {
|
||||||
|
setFiles((prev) => [...prev, ...acceptedFiles]);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const { getRootProps, getInputProps, isDragActive } = useDropzone({
|
||||||
|
onDrop,
|
||||||
|
accept: {
|
||||||
|
'application/pdf': ['.pdf'],
|
||||||
|
'image/*': ['.png', '.jpg', '.jpeg', '.webp'],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const removeFile = (index: number) => {
|
||||||
|
setFiles((prev) => prev.filter((_, i) => i !== index));
|
||||||
|
};
|
||||||
|
|
||||||
|
const clearFiles = () => {
|
||||||
|
setFiles([]);
|
||||||
|
setResult(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
const getToken = () => {
|
||||||
|
return localStorage.getItem('token');
|
||||||
|
};
|
||||||
|
|
||||||
|
const processFiles = async () => {
|
||||||
|
if (files.length === 0) {
|
||||||
|
toast.error('Selecione pelo menos um arquivo');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setProcessing(true);
|
||||||
|
setResult(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const formData = new FormData();
|
||||||
|
files.forEach((file) => {
|
||||||
|
formData.append('files', file);
|
||||||
|
});
|
||||||
|
|
||||||
|
const token = getToken();
|
||||||
|
const config = {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'multipart/form-data',
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let response;
|
||||||
|
|
||||||
|
switch (selectedOperation) {
|
||||||
|
case 'compress':
|
||||||
|
formData.append('quality', compressQuality.toString());
|
||||||
|
response = await axios.post('/api/pdf/compress', formData, config);
|
||||||
|
toast.success('PDFs comprimidos com sucesso!');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'split':
|
||||||
|
formData.append('splitType', splitType);
|
||||||
|
if (splitType === 'pages') {
|
||||||
|
const pages = splitPages.split(',').map((p) => parseInt(p.trim()));
|
||||||
|
formData.append('pages', JSON.stringify(pages));
|
||||||
|
} else if (splitType === 'range') {
|
||||||
|
const ranges = splitRanges.split(',').map((r) => {
|
||||||
|
const [start, end] = r.trim().split('-').map(Number);
|
||||||
|
return { start, end };
|
||||||
|
});
|
||||||
|
formData.append('ranges', JSON.stringify(ranges));
|
||||||
|
} else {
|
||||||
|
formData.append('chunkSize', chunkSize.toString());
|
||||||
|
}
|
||||||
|
response = await axios.post('/api/pdf/split', formData, config);
|
||||||
|
toast.success('PDF dividido com sucesso!');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'merge':
|
||||||
|
response = await axios.post('/api/pdf/merge', formData, config);
|
||||||
|
toast.success('PDFs mesclados com sucesso!');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'pdfToImages':
|
||||||
|
formData.append('format', imageFormat);
|
||||||
|
formData.append('quality', imageQuality.toString());
|
||||||
|
formData.append('density', imageDensity.toString());
|
||||||
|
response = await axios.post('/api/pdf/pdf-to-images', formData, config);
|
||||||
|
toast.success('PDF convertido em imagens!');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'imagesToPDF':
|
||||||
|
formData.append('fitToPage', 'true');
|
||||||
|
response = await axios.post('/api/pdf/images-to-pdf', formData, config);
|
||||||
|
toast.success('Imagens convertidas em PDF!');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'ocr':
|
||||||
|
formData.append('language', ocrLanguage);
|
||||||
|
response = await axios.post('/api/pdf/ocr', formData, config);
|
||||||
|
toast.success('OCR realizado com sucesso!');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'info':
|
||||||
|
response = await axios.post('/api/pdf/info', formData, config);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
setResult(response.data);
|
||||||
|
loadHistory();
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('Erro ao processar arquivos:', error);
|
||||||
|
toast.error(error.response?.data?.message || 'Erro ao processar arquivos');
|
||||||
|
} finally {
|
||||||
|
setProcessing(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const loadHistory = async () => {
|
||||||
|
try {
|
||||||
|
const token = getToken();
|
||||||
|
const response = await axios.get('/api/pdf/history', {
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
|
});
|
||||||
|
setHistory(response.data.operacoes || []);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao carregar histórico:', error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const downloadFile = async (operacaoId: string, fileIndex: number) => {
|
||||||
|
try {
|
||||||
|
const token = getToken();
|
||||||
|
const response = await axios.get(`/api/pdf/download/${operacaoId}/${fileIndex}`, {
|
||||||
|
headers: { Authorization: `Bearer ${token}` },
|
||||||
|
responseType: 'blob',
|
||||||
|
});
|
||||||
|
|
||||||
|
const url = window.URL.createObjectURL(new Blob([response.data]));
|
||||||
|
const link = document.createElement('a');
|
||||||
|
link.href = url;
|
||||||
|
link.setAttribute('download', `arquivo-${fileIndex}.pdf`);
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
link.remove();
|
||||||
|
|
||||||
|
toast.success('Download iniciado!');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao fazer download:', error);
|
||||||
|
toast.error('Erro ao fazer download');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
loadHistory();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const operations = [
|
||||||
|
{
|
||||||
|
id: 'compress' as OperationType,
|
||||||
|
name: 'Comprimir PDF',
|
||||||
|
icon: ArrowsPointingInIcon,
|
||||||
|
description: 'Reduza o tamanho mantendo a qualidade',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'split' as OperationType,
|
||||||
|
name: 'Dividir PDF',
|
||||||
|
icon: ScissorsIcon,
|
||||||
|
description: 'Separe páginas específicas',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'merge' as OperationType,
|
||||||
|
name: 'Mesclar PDFs',
|
||||||
|
icon: ArrowsUpDownIcon,
|
||||||
|
description: 'Combine múltiplos PDFs',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'pdfToImages' as OperationType,
|
||||||
|
name: 'PDF para Imagens',
|
||||||
|
icon: PhotoIcon,
|
||||||
|
description: 'Converta páginas em imagens',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'imagesToPDF' as OperationType,
|
||||||
|
name: 'Imagens para PDF',
|
||||||
|
icon: DocumentIcon,
|
||||||
|
description: 'Crie PDF a partir de imagens',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'ocr' as OperationType,
|
||||||
|
name: 'OCR',
|
||||||
|
icon: DocumentTextIcon,
|
||||||
|
description: 'Extraia texto de PDFs',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'info' as OperationType,
|
||||||
|
name: 'Informações',
|
||||||
|
icon: InformationCircleIcon,
|
||||||
|
description: 'Obtenha detalhes do PDF',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const formatBytes = (bytes: number) => {
|
||||||
|
if (bytes === 0) return '0 Bytes';
|
||||||
|
const k = 1024;
|
||||||
|
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i];
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="min-h-screen bg-gray-50 py-8">
|
||||||
|
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
|
||||||
|
<div className="mb-8">
|
||||||
|
<h1 className="text-3xl font-bold text-gray-900">Manipulação de PDFs</h1>
|
||||||
|
<p className="mt-2 text-gray-600">
|
||||||
|
Ferramentas completas para trabalhar com arquivos PDF
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Seleção de operação */}
|
||||||
|
<div className="grid grid-cols-2 md:grid-cols-4 lg:grid-cols-7 gap-4 mb-8">
|
||||||
|
{operations.map((op) => (
|
||||||
|
<button
|
||||||
|
key={op.id}
|
||||||
|
onClick={() => {
|
||||||
|
setSelectedOperation(op.id);
|
||||||
|
clearFiles();
|
||||||
|
}}
|
||||||
|
className={`p-4 rounded-lg border-2 transition-all ${
|
||||||
|
selectedOperation === op.id
|
||||||
|
? 'border-blue-500 bg-blue-50 text-blue-700'
|
||||||
|
: 'border-gray-200 bg-white hover:border-gray-300'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<op.icon className="h-8 w-8 mx-auto mb-2" />
|
||||||
|
<div className="text-sm font-medium">{op.name}</div>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-8">
|
||||||
|
{/* Área principal */}
|
||||||
|
<div className="lg:col-span-2 space-y-6">
|
||||||
|
{/* Upload de arquivos */}
|
||||||
|
<div className="bg-white rounded-lg shadow p-6">
|
||||||
|
<h2 className="text-xl font-semibold mb-4">Arquivos</h2>
|
||||||
|
|
||||||
|
<div
|
||||||
|
{...getRootProps()}
|
||||||
|
className={`border-2 border-dashed rounded-lg p-12 text-center cursor-pointer transition-colors ${
|
||||||
|
isDragActive
|
||||||
|
? 'border-blue-500 bg-blue-50'
|
||||||
|
: 'border-gray-300 hover:border-gray-400'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<input {...getInputProps()} />
|
||||||
|
<CloudArrowUpIcon className="h-12 w-12 mx-auto text-gray-400 mb-4" />
|
||||||
|
{isDragActive ? (
|
||||||
|
<p className="text-blue-600">Solte os arquivos aqui...</p>
|
||||||
|
) : (
|
||||||
|
<div>
|
||||||
|
<p className="text-gray-600 mb-2">
|
||||||
|
Arraste e solte arquivos aqui, ou clique para selecionar
|
||||||
|
</p>
|
||||||
|
<p className="text-sm text-gray-500">
|
||||||
|
PDFs e imagens (PNG, JPG, WEBP)
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Lista de arquivos */}
|
||||||
|
{files.length > 0 && (
|
||||||
|
<div className="mt-4">
|
||||||
|
<div className="flex justify-between items-center mb-2">
|
||||||
|
<h3 className="font-medium">
|
||||||
|
Arquivos selecionados ({files.length})
|
||||||
|
</h3>
|
||||||
|
<button
|
||||||
|
onClick={clearFiles}
|
||||||
|
className="text-sm text-red-600 hover:text-red-700"
|
||||||
|
>
|
||||||
|
Limpar todos
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div className="space-y-2">
|
||||||
|
{files.map((file, index) => (
|
||||||
|
<div
|
||||||
|
key={index}
|
||||||
|
className="flex items-center justify-between p-3 bg-gray-50 rounded"
|
||||||
|
>
|
||||||
|
<div className="flex items-center space-x-3">
|
||||||
|
<DocumentIcon className="h-5 w-5 text-gray-400" />
|
||||||
|
<div>
|
||||||
|
<p className="text-sm font-medium">{file.name}</p>
|
||||||
|
<p className="text-xs text-gray-500">
|
||||||
|
{formatBytes(file.size)}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
onClick={() => removeFile(index)}
|
||||||
|
className="text-red-600 hover:text-red-700"
|
||||||
|
>
|
||||||
|
Remover
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Configurações específicas */}
|
||||||
|
<div className="bg-white rounded-lg shadow p-6">
|
||||||
|
<h2 className="text-xl font-semibold mb-4">Configurações</h2>
|
||||||
|
|
||||||
|
{selectedOperation === 'compress' && (
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">
|
||||||
|
Qualidade ({compressQuality}%)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="range"
|
||||||
|
min="50"
|
||||||
|
max="100"
|
||||||
|
value={compressQuality}
|
||||||
|
onChange={(e) => setCompressQuality(parseInt(e.target.value))}
|
||||||
|
className="w-full"
|
||||||
|
/>
|
||||||
|
<p className="text-xs text-gray-500 mt-1">
|
||||||
|
95% recomendado para qualidade sem perda perceptível
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{selectedOperation === 'split' && (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">
|
||||||
|
Tipo de divisão
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
value={splitType}
|
||||||
|
onChange={(e) => setSplitType(e.target.value as any)}
|
||||||
|
className="w-full border border-gray-300 rounded-lg px-3 py-2"
|
||||||
|
>
|
||||||
|
<option value="pages">Páginas específicas</option>
|
||||||
|
<option value="range">Intervalos</option>
|
||||||
|
<option value="chunks">Chunks (partes iguais)</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{splitType === 'pages' && (
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">
|
||||||
|
Páginas (separadas por vírgula)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={splitPages}
|
||||||
|
onChange={(e) => setSplitPages(e.target.value)}
|
||||||
|
placeholder="1,2,3,5,8"
|
||||||
|
className="w-full border border-gray-300 rounded-lg px-3 py-2"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{splitType === 'range' && (
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">
|
||||||
|
Intervalos (ex: 1-5,6-10)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={splitRanges}
|
||||||
|
onChange={(e) => setSplitRanges(e.target.value)}
|
||||||
|
placeholder="1-5,6-10,11-15"
|
||||||
|
className="w-full border border-gray-300 rounded-lg px-3 py-2"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{splitType === 'chunks' && (
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">
|
||||||
|
Páginas por chunk
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={chunkSize}
|
||||||
|
onChange={(e) => setChunkSize(parseInt(e.target.value))}
|
||||||
|
min="1"
|
||||||
|
className="w-full border border-gray-300 rounded-lg px-3 py-2"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{selectedOperation === 'pdfToImages' && (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">Formato</label>
|
||||||
|
<select
|
||||||
|
value={imageFormat}
|
||||||
|
onChange={(e) => setImageFormat(e.target.value as any)}
|
||||||
|
className="w-full border border-gray-300 rounded-lg px-3 py-2"
|
||||||
|
>
|
||||||
|
<option value="png">PNG</option>
|
||||||
|
<option value="jpg">JPG</option>
|
||||||
|
<option value="webp">WEBP</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">
|
||||||
|
Qualidade ({imageQuality}%)
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="range"
|
||||||
|
min="50"
|
||||||
|
max="100"
|
||||||
|
value={imageQuality}
|
||||||
|
onChange={(e) => setImageQuality(parseInt(e.target.value))}
|
||||||
|
className="w-full"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">
|
||||||
|
DPI ({imageDensity})
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="range"
|
||||||
|
min="150"
|
||||||
|
max="600"
|
||||||
|
step="50"
|
||||||
|
value={imageDensity}
|
||||||
|
onChange={(e) => setImageDensity(parseInt(e.target.value))}
|
||||||
|
className="w-full"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{selectedOperation === 'ocr' && (
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium mb-2">Idioma</label>
|
||||||
|
<select
|
||||||
|
value={ocrLanguage}
|
||||||
|
onChange={(e) => setOcrLanguage(e.target.value)}
|
||||||
|
className="w-full border border-gray-300 rounded-lg px-3 py-2"
|
||||||
|
>
|
||||||
|
<option value="por">Português</option>
|
||||||
|
<option value="eng">Inglês</option>
|
||||||
|
<option value="spa">Espanhol</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<button
|
||||||
|
onClick={processFiles}
|
||||||
|
disabled={processing || files.length === 0}
|
||||||
|
className={`mt-6 w-full py-3 rounded-lg font-medium transition-colors ${
|
||||||
|
processing || files.length === 0
|
||||||
|
? 'bg-gray-300 text-gray-500 cursor-not-allowed'
|
||||||
|
: 'bg-blue-600 text-white hover:bg-blue-700'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{processing ? (
|
||||||
|
<span className="flex items-center justify-center">
|
||||||
|
<svg
|
||||||
|
className="animate-spin h-5 w-5 mr-2"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<circle
|
||||||
|
className="opacity-25"
|
||||||
|
cx="12"
|
||||||
|
cy="12"
|
||||||
|
r="10"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="4"
|
||||||
|
></circle>
|
||||||
|
<path
|
||||||
|
className="opacity-75"
|
||||||
|
fill="currentColor"
|
||||||
|
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||||
|
></path>
|
||||||
|
</svg>
|
||||||
|
Processando...
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
'Processar'
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Resultado */}
|
||||||
|
{result && (
|
||||||
|
<div className="bg-white rounded-lg shadow p-6">
|
||||||
|
<h2 className="text-xl font-semibold mb-4">Resultado</h2>
|
||||||
|
|
||||||
|
{selectedOperation === 'compress' && result.reduction !== undefined && (
|
||||||
|
<div className="space-y-2">
|
||||||
|
<p className="text-green-600 font-medium">
|
||||||
|
Redução de {result.reduction.toFixed(2)}%
|
||||||
|
</p>
|
||||||
|
<p className="text-sm text-gray-600">
|
||||||
|
Tempo de processamento: {result.processingTime}ms
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{(result.outputPaths || result.outputPath) && (
|
||||||
|
<div className="mt-4">
|
||||||
|
<p className="font-medium mb-2">Arquivos gerados:</p>
|
||||||
|
{result.outputPaths?.map((path, index) => (
|
||||||
|
<button
|
||||||
|
key={index}
|
||||||
|
onClick={() => downloadFile(result.operacaoId, index)}
|
||||||
|
className="flex items-center space-x-2 w-full p-2 hover:bg-gray-50 rounded"
|
||||||
|
>
|
||||||
|
<ArrowDownTrayIcon className="h-5 w-5 text-blue-600" />
|
||||||
|
<span className="text-sm">Arquivo {index + 1}</span>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
{result.outputPath && (
|
||||||
|
<button
|
||||||
|
onClick={() => downloadFile(result.operacaoId, 0)}
|
||||||
|
className="flex items-center space-x-2 w-full p-2 hover:bg-gray-50 rounded"
|
||||||
|
>
|
||||||
|
<ArrowDownTrayIcon className="h-5 w-5 text-blue-600" />
|
||||||
|
<span className="text-sm">Download</span>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{selectedOperation === 'ocr' && result.text && (
|
||||||
|
<div className="mt-4">
|
||||||
|
<p className="font-medium mb-2">Texto extraído:</p>
|
||||||
|
<div className="bg-gray-50 p-4 rounded max-h-64 overflow-y-auto">
|
||||||
|
<pre className="text-sm whitespace-pre-wrap">{result.text}</pre>
|
||||||
|
</div>
|
||||||
|
{result.confidence && (
|
||||||
|
<p className="text-sm text-gray-600 mt-2">
|
||||||
|
Confiança: {result.confidence.toFixed(2)}%
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Histórico */}
|
||||||
|
<div className="lg:col-span-1">
|
||||||
|
<div className="bg-white rounded-lg shadow p-6 sticky top-8">
|
||||||
|
<h2 className="text-xl font-semibold mb-4">Histórico</h2>
|
||||||
|
|
||||||
|
{history.length === 0 ? (
|
||||||
|
<p className="text-gray-500 text-sm">Nenhuma operação ainda</p>
|
||||||
|
) : (
|
||||||
|
<div className="space-y-3 max-h-96 overflow-y-auto">
|
||||||
|
{history.map((op) => (
|
||||||
|
<div key={op.id} className="border-b pb-3">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span className="text-sm font-medium">
|
||||||
|
{op.tipoOperacao.replace('_', ' ')}
|
||||||
|
</span>
|
||||||
|
<span
|
||||||
|
className={`text-xs px-2 py-1 rounded ${
|
||||||
|
op.status === 'CONCLUIDO'
|
||||||
|
? 'bg-green-100 text-green-800'
|
||||||
|
: op.status === 'ERRO'
|
||||||
|
? 'bg-red-100 text-red-800'
|
||||||
|
: 'bg-yellow-100 text-yellow-800'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{op.status}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center text-xs text-gray-500 mt-1">
|
||||||
|
<ClockIcon className="h-3 w-3 mr-1" />
|
||||||
|
{new Date(op.criadoEm).toLocaleString('pt-BR')}
|
||||||
|
</div>
|
||||||
|
{op.tempoProcessamento && (
|
||||||
|
<p className="text-xs text-gray-500 mt-1">
|
||||||
|
{op.tempoProcessamento}ms
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -0,0 +1,588 @@
|
|||||||
|
import { PDFDocument } from 'pdf-lib';
|
||||||
|
import sharp from 'sharp';
|
||||||
|
import { createWorker } from 'tesseract.js';
|
||||||
|
import { fromPath } from 'pdf2pic';
|
||||||
|
import fs from 'fs/promises';
|
||||||
|
import path from 'path';
|
||||||
|
import { v4 as uuidv4 } from 'uuid';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serviço completo de manipulação de PDFs
|
||||||
|
* Implementa compressão sem perda, divisão, mesclagem, conversão e OCR
|
||||||
|
*/
|
||||||
|
|
||||||
|
interface CompressOptions {
|
||||||
|
quality?: number; // 0-100, padrão 95 para qualidade alta
|
||||||
|
maxSizeMB?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SplitOptions {
|
||||||
|
splitType: 'pages' | 'range' | 'chunks';
|
||||||
|
pages?: number[]; // Páginas específicas para extrair
|
||||||
|
ranges?: Array<{ start: number; end: number }>; // Intervalos de páginas
|
||||||
|
chunkSize?: number; // Tamanho de cada chunk em páginas
|
||||||
|
}
|
||||||
|
|
||||||
|
interface MergeOptions {
|
||||||
|
filePaths: string[];
|
||||||
|
outputPath: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PDFToImageOptions {
|
||||||
|
format?: 'png' | 'jpg' | 'webp';
|
||||||
|
quality?: number;
|
||||||
|
density?: number; // DPI, padrão 300
|
||||||
|
pages?: number[] | 'all'; // Páginas específicas ou todas
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ImageToPDFOptions {
|
||||||
|
imagePaths: string[];
|
||||||
|
outputPath: string;
|
||||||
|
pageSize?: { width: number; height: number };
|
||||||
|
fitToPage?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OCROptions {
|
||||||
|
language?: string; // padrão 'por' (português)
|
||||||
|
pages?: number[] | 'all';
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OCRResult {
|
||||||
|
text: string;
|
||||||
|
confidence: number;
|
||||||
|
pages: Array<{
|
||||||
|
pageNumber: number;
|
||||||
|
text: string;
|
||||||
|
confidence: number;
|
||||||
|
}>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PDFService {
|
||||||
|
private static readonly TEMP_DIR = path.join(process.cwd(), 'storage', 'temp', 'pdf-ops');
|
||||||
|
private static readonly OUTPUT_DIR = path.join(process.cwd(), 'storage', 'pdf-output');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inicializa os diretórios necessários
|
||||||
|
*/
|
||||||
|
static async initialize(): Promise<void> {
|
||||||
|
try {
|
||||||
|
await fs.mkdir(this.TEMP_DIR, { recursive: true });
|
||||||
|
await fs.mkdir(this.OUTPUT_DIR, { recursive: true });
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao criar diretórios:', error);
|
||||||
|
throw new Error('Falha ao inicializar diretórios para operações de PDF');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comprime um PDF mantendo a qualidade máxima possível
|
||||||
|
* Usa otimização de streams e remoção de metadados desnecessários
|
||||||
|
*/
|
||||||
|
static async compressPDF(
|
||||||
|
inputPath: string,
|
||||||
|
outputPath?: string,
|
||||||
|
options: CompressOptions = {}
|
||||||
|
): Promise<{ outputPath: string; originalSize: number; compressedSize: number; reduction: number }> {
|
||||||
|
try {
|
||||||
|
await this.initialize();
|
||||||
|
|
||||||
|
const { quality = 95 } = options;
|
||||||
|
|
||||||
|
// Lê o PDF original
|
||||||
|
const pdfBytes = await fs.readFile(inputPath);
|
||||||
|
const originalSize = pdfBytes.length;
|
||||||
|
|
||||||
|
// Carrega o PDF
|
||||||
|
const pdfDoc = await PDFDocument.load(pdfBytes, {
|
||||||
|
updateMetadata: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove metadados desnecessários mas mantém metadados essenciais
|
||||||
|
pdfDoc.setTitle('');
|
||||||
|
pdfDoc.setAuthor('');
|
||||||
|
pdfDoc.setSubject('');
|
||||||
|
pdfDoc.setKeywords([]);
|
||||||
|
pdfDoc.setProducer('');
|
||||||
|
pdfDoc.setCreator('');
|
||||||
|
|
||||||
|
// Salva com compressão
|
||||||
|
const compressedBytes = await pdfDoc.save({
|
||||||
|
useObjectStreams: true, // Comprime streams de objetos
|
||||||
|
addDefaultPage: false,
|
||||||
|
objectsPerTick: 50,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Se a compressão não foi suficiente, otimiza as imagens
|
||||||
|
let finalBytes = compressedBytes;
|
||||||
|
|
||||||
|
if (options.maxSizeMB && compressedBytes.length > options.maxSizeMB * 1024 * 1024) {
|
||||||
|
// Recarrega para otimizar imagens
|
||||||
|
const reloadedDoc = await PDFDocument.load(compressedBytes);
|
||||||
|
const pages = reloadedDoc.getPages();
|
||||||
|
|
||||||
|
// Processa cada página para otimizar imagens
|
||||||
|
for (const page of pages) {
|
||||||
|
// A otimização de imagens seria feita aqui
|
||||||
|
// Por limitações da pdf-lib, mantemos a qualidade original
|
||||||
|
}
|
||||||
|
|
||||||
|
finalBytes = await reloadedDoc.save({
|
||||||
|
useObjectStreams: true,
|
||||||
|
addDefaultPage: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Define o caminho de saída
|
||||||
|
const finalOutputPath = outputPath || path.join(
|
||||||
|
this.OUTPUT_DIR,
|
||||||
|
`compressed-${uuidv4()}.pdf`
|
||||||
|
);
|
||||||
|
|
||||||
|
// Salva o arquivo comprimido
|
||||||
|
await fs.writeFile(finalOutputPath, finalBytes);
|
||||||
|
|
||||||
|
const compressedSize = finalBytes.length;
|
||||||
|
const reduction = ((originalSize - compressedSize) / originalSize) * 100;
|
||||||
|
|
||||||
|
return {
|
||||||
|
outputPath: finalOutputPath,
|
||||||
|
originalSize,
|
||||||
|
compressedSize,
|
||||||
|
reduction: Math.max(0, reduction),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao comprimir PDF:', error);
|
||||||
|
throw new Error(`Falha ao comprimir PDF: ${error instanceof Error ? error.message : 'Erro desconhecido'}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Divide um PDF em múltiplos arquivos
|
||||||
|
*/
|
||||||
|
static async splitPDF(
|
||||||
|
inputPath: string,
|
||||||
|
options: SplitOptions
|
||||||
|
): Promise<string[]> {
|
||||||
|
try {
|
||||||
|
await this.initialize();
|
||||||
|
|
||||||
|
const pdfBytes = await fs.readFile(inputPath);
|
||||||
|
const pdfDoc = await PDFDocument.load(pdfBytes);
|
||||||
|
const totalPages = pdfDoc.getPageCount();
|
||||||
|
const outputPaths: string[] = [];
|
||||||
|
|
||||||
|
if (options.splitType === 'pages' && options.pages) {
|
||||||
|
// Divide em páginas específicas
|
||||||
|
for (const pageNum of options.pages) {
|
||||||
|
if (pageNum < 1 || pageNum > totalPages) {
|
||||||
|
throw new Error(`Página ${pageNum} inválida. O PDF tem ${totalPages} páginas.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const newDoc = await PDFDocument.create();
|
||||||
|
const [copiedPage] = await newDoc.copyPages(pdfDoc, [pageNum - 1]);
|
||||||
|
newDoc.addPage(copiedPage);
|
||||||
|
|
||||||
|
const outputPath = path.join(this.OUTPUT_DIR, `split-page-${pageNum}-${uuidv4()}.pdf`);
|
||||||
|
const newPdfBytes = await newDoc.save();
|
||||||
|
await fs.writeFile(outputPath, newPdfBytes);
|
||||||
|
outputPaths.push(outputPath);
|
||||||
|
}
|
||||||
|
} else if (options.splitType === 'range' && options.ranges) {
|
||||||
|
// Divide em intervalos de páginas
|
||||||
|
for (let i = 0; i < options.ranges.length; i++) {
|
||||||
|
const { start, end } = options.ranges[i];
|
||||||
|
|
||||||
|
if (start < 1 || end > totalPages || start > end) {
|
||||||
|
throw new Error(`Intervalo inválido: ${start}-${end}. O PDF tem ${totalPages} páginas.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const newDoc = await PDFDocument.create();
|
||||||
|
const pageIndices = Array.from({ length: end - start + 1 }, (_, idx) => start - 1 + idx);
|
||||||
|
const copiedPages = await newDoc.copyPages(pdfDoc, pageIndices);
|
||||||
|
copiedPages.forEach((page) => newDoc.addPage(page));
|
||||||
|
|
||||||
|
const outputPath = path.join(this.OUTPUT_DIR, `split-range-${start}-${end}-${uuidv4()}.pdf`);
|
||||||
|
const newPdfBytes = await newDoc.save();
|
||||||
|
await fs.writeFile(outputPath, newPdfBytes);
|
||||||
|
outputPaths.push(outputPath);
|
||||||
|
}
|
||||||
|
} else if (options.splitType === 'chunks' && options.chunkSize) {
|
||||||
|
// Divide em chunks de tamanho fixo
|
||||||
|
const chunkSize = options.chunkSize;
|
||||||
|
const numChunks = Math.ceil(totalPages / chunkSize);
|
||||||
|
|
||||||
|
for (let i = 0; i < numChunks; i++) {
|
||||||
|
const startPage = i * chunkSize;
|
||||||
|
const endPage = Math.min((i + 1) * chunkSize, totalPages);
|
||||||
|
|
||||||
|
const newDoc = await PDFDocument.create();
|
||||||
|
const pageIndices = Array.from({ length: endPage - startPage }, (_, idx) => startPage + idx);
|
||||||
|
const copiedPages = await newDoc.copyPages(pdfDoc, pageIndices);
|
||||||
|
copiedPages.forEach((page) => newDoc.addPage(page));
|
||||||
|
|
||||||
|
const outputPath = path.join(this.OUTPUT_DIR, `split-chunk-${i + 1}-${uuidv4()}.pdf`);
|
||||||
|
const newPdfBytes = await newDoc.save();
|
||||||
|
await fs.writeFile(outputPath, newPdfBytes);
|
||||||
|
outputPaths.push(outputPath);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
throw new Error('Opções de divisão inválidas');
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputPaths;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao dividir PDF:', error);
|
||||||
|
throw new Error(`Falha ao dividir PDF: ${error instanceof Error ? error.message : 'Erro desconhecido'}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mescla múltiplos PDFs em um único arquivo
|
||||||
|
*/
|
||||||
|
static async mergePDFs(options: MergeOptions): Promise<string> {
|
||||||
|
try {
|
||||||
|
await this.initialize();
|
||||||
|
|
||||||
|
if (!options.filePaths || options.filePaths.length === 0) {
|
||||||
|
throw new Error('Nenhum arquivo fornecido para mesclagem');
|
||||||
|
}
|
||||||
|
|
||||||
|
const mergedDoc = await PDFDocument.create();
|
||||||
|
|
||||||
|
// Mescla cada PDF
|
||||||
|
for (const filePath of options.filePaths) {
|
||||||
|
try {
|
||||||
|
const pdfBytes = await fs.readFile(filePath);
|
||||||
|
const pdfDoc = await PDFDocument.load(pdfBytes);
|
||||||
|
const copiedPages = await mergedDoc.copyPages(pdfDoc, pdfDoc.getPageIndices());
|
||||||
|
copiedPages.forEach((page) => mergedDoc.addPage(page));
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Erro ao processar arquivo ${filePath}:`, error);
|
||||||
|
throw new Error(`Falha ao processar arquivo ${path.basename(filePath)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const outputPath = options.outputPath || path.join(
|
||||||
|
this.OUTPUT_DIR,
|
||||||
|
`merged-${uuidv4()}.pdf`
|
||||||
|
);
|
||||||
|
|
||||||
|
const mergedBytes = await mergedDoc.save();
|
||||||
|
await fs.writeFile(outputPath, mergedBytes);
|
||||||
|
|
||||||
|
return outputPath;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao mesclar PDFs:', error);
|
||||||
|
throw new Error(`Falha ao mesclar PDFs: ${error instanceof Error ? error.message : 'Erro desconhecido'}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converte páginas de PDF em imagens
|
||||||
|
*/
|
||||||
|
static async pdfToImages(
|
||||||
|
inputPath: string,
|
||||||
|
options: PDFToImageOptions = {}
|
||||||
|
): Promise<string[]> {
|
||||||
|
try {
|
||||||
|
await this.initialize();
|
||||||
|
|
||||||
|
const {
|
||||||
|
format = 'png',
|
||||||
|
quality = 95,
|
||||||
|
density = 300,
|
||||||
|
pages = 'all',
|
||||||
|
} = options;
|
||||||
|
|
||||||
|
const pdfBytes = await fs.readFile(inputPath);
|
||||||
|
const pdfDoc = await PDFDocument.load(pdfBytes);
|
||||||
|
const totalPages = pdfDoc.getPageCount();
|
||||||
|
|
||||||
|
// Determina quais páginas converter
|
||||||
|
const pagesToConvert = pages === 'all'
|
||||||
|
? Array.from({ length: totalPages }, (_, i) => i + 1)
|
||||||
|
: pages;
|
||||||
|
|
||||||
|
const outputPaths: string[] = [];
|
||||||
|
|
||||||
|
// Configura o conversor
|
||||||
|
const converter = fromPath(inputPath, {
|
||||||
|
density,
|
||||||
|
saveFilename: uuidv4(),
|
||||||
|
savePath: this.OUTPUT_DIR,
|
||||||
|
format,
|
||||||
|
width: 2480, // A4 em 300 DPI
|
||||||
|
height: 3508,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Converte cada página
|
||||||
|
for (const pageNum of pagesToConvert) {
|
||||||
|
if (pageNum < 1 || pageNum > totalPages) {
|
||||||
|
console.warn(`Página ${pageNum} ignorada - fora do intervalo`);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await converter(pageNum, { responseType: 'image' });
|
||||||
|
|
||||||
|
if (result && typeof result === 'object' && 'path' in result) {
|
||||||
|
const imagePath = result.path as string;
|
||||||
|
|
||||||
|
// Otimiza a imagem com sharp
|
||||||
|
const optimizedPath = path.join(
|
||||||
|
this.OUTPUT_DIR,
|
||||||
|
`page-${pageNum}-${uuidv4()}.${format}`
|
||||||
|
);
|
||||||
|
|
||||||
|
if (format === 'jpg') {
|
||||||
|
await sharp(imagePath)
|
||||||
|
.jpeg({ quality, progressive: true })
|
||||||
|
.toFile(optimizedPath);
|
||||||
|
} else if (format === 'png') {
|
||||||
|
await sharp(imagePath)
|
||||||
|
.png({ quality, progressive: true, compressionLevel: 9 })
|
||||||
|
.toFile(optimizedPath);
|
||||||
|
} else if (format === 'webp') {
|
||||||
|
await sharp(imagePath)
|
||||||
|
.webp({ quality })
|
||||||
|
.toFile(optimizedPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove o arquivo temporário
|
||||||
|
await fs.unlink(imagePath).catch(() => {});
|
||||||
|
|
||||||
|
outputPaths.push(optimizedPath);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Erro ao converter página ${pageNum}:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputPaths;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao converter PDF para imagens:', error);
|
||||||
|
throw new Error(`Falha ao converter PDF para imagens: ${error instanceof Error ? error.message : 'Erro desconhecido'}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converte imagens em PDF
|
||||||
|
*/
|
||||||
|
static async imagesToPDF(options: ImageToPDFOptions): Promise<string> {
|
||||||
|
try {
|
||||||
|
await this.initialize();
|
||||||
|
|
||||||
|
if (!options.imagePaths || options.imagePaths.length === 0) {
|
||||||
|
throw new Error('Nenhuma imagem fornecida para conversão');
|
||||||
|
}
|
||||||
|
|
||||||
|
const pdfDoc = await PDFDocument.create();
|
||||||
|
|
||||||
|
for (const imagePath of options.imagePaths) {
|
||||||
|
try {
|
||||||
|
const imageBytes = await fs.readFile(imagePath);
|
||||||
|
const ext = path.extname(imagePath).toLowerCase();
|
||||||
|
|
||||||
|
let image;
|
||||||
|
if (ext === '.png') {
|
||||||
|
image = await pdfDoc.embedPng(imageBytes);
|
||||||
|
} else if (ext === '.jpg' || ext === '.jpeg') {
|
||||||
|
image = await pdfDoc.embedJpg(imageBytes);
|
||||||
|
} else {
|
||||||
|
// Converte outros formatos para PNG primeiro
|
||||||
|
const pngBuffer = await sharp(imageBytes).png().toBuffer();
|
||||||
|
image = await pdfDoc.embedPng(pngBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { width, height } = image;
|
||||||
|
|
||||||
|
// Define o tamanho da página
|
||||||
|
let pageWidth = width;
|
||||||
|
let pageHeight = height;
|
||||||
|
|
||||||
|
if (options.pageSize) {
|
||||||
|
pageWidth = options.pageSize.width;
|
||||||
|
pageHeight = options.pageSize.height;
|
||||||
|
}
|
||||||
|
|
||||||
|
const page = pdfDoc.addPage([pageWidth, pageHeight]);
|
||||||
|
|
||||||
|
// Calcula dimensões para ajustar à página se necessário
|
||||||
|
if (options.fitToPage) {
|
||||||
|
const scale = Math.min(pageWidth / width, pageHeight / height);
|
||||||
|
const scaledWidth = width * scale;
|
||||||
|
const scaledHeight = height * scale;
|
||||||
|
const x = (pageWidth - scaledWidth) / 2;
|
||||||
|
const y = (pageHeight - scaledHeight) / 2;
|
||||||
|
|
||||||
|
page.drawImage(image, {
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width: scaledWidth,
|
||||||
|
height: scaledHeight,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
page.drawImage(image, {
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
width: pageWidth,
|
||||||
|
height: pageHeight,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Erro ao processar imagem ${imagePath}:`, error);
|
||||||
|
throw new Error(`Falha ao processar imagem ${path.basename(imagePath)}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const outputPath = options.outputPath || path.join(
|
||||||
|
this.OUTPUT_DIR,
|
||||||
|
`images-to-pdf-${uuidv4()}.pdf`
|
||||||
|
);
|
||||||
|
|
||||||
|
const pdfBytes = await pdfDoc.save();
|
||||||
|
await fs.writeFile(outputPath, pdfBytes);
|
||||||
|
|
||||||
|
return outputPath;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao converter imagens para PDF:', error);
|
||||||
|
throw new Error(`Falha ao converter imagens para PDF: ${error instanceof Error ? error.message : 'Erro desconhecido'}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Realiza OCR em um PDF e extrai o texto
|
||||||
|
*/
|
||||||
|
static async performOCR(
|
||||||
|
inputPath: string,
|
||||||
|
options: OCROptions = {}
|
||||||
|
): Promise<OCRResult> {
|
||||||
|
try {
|
||||||
|
await this.initialize();
|
||||||
|
|
||||||
|
const { language = 'por', pages = 'all' } = options;
|
||||||
|
|
||||||
|
// Primeiro converte o PDF em imagens
|
||||||
|
const imageOptions: PDFToImageOptions = {
|
||||||
|
format: 'png',
|
||||||
|
density: 300,
|
||||||
|
pages,
|
||||||
|
};
|
||||||
|
|
||||||
|
const imagePaths = await this.pdfToImages(inputPath, imageOptions);
|
||||||
|
|
||||||
|
// Cria um worker do Tesseract
|
||||||
|
const worker = await createWorker(language);
|
||||||
|
|
||||||
|
const pageResults: Array<{
|
||||||
|
pageNumber: number;
|
||||||
|
text: string;
|
||||||
|
confidence: number;
|
||||||
|
}> = [];
|
||||||
|
|
||||||
|
let allText = '';
|
||||||
|
let totalConfidence = 0;
|
||||||
|
|
||||||
|
// Processa cada imagem
|
||||||
|
for (let i = 0; i < imagePaths.length; i++) {
|
||||||
|
try {
|
||||||
|
const imagePath = imagePaths[i];
|
||||||
|
const { data } = await worker.recognize(imagePath);
|
||||||
|
|
||||||
|
pageResults.push({
|
||||||
|
pageNumber: i + 1,
|
||||||
|
text: data.text,
|
||||||
|
confidence: data.confidence,
|
||||||
|
});
|
||||||
|
|
||||||
|
allText += data.text + '\n\n';
|
||||||
|
totalConfidence += data.confidence;
|
||||||
|
|
||||||
|
// Remove a imagem temporária
|
||||||
|
await fs.unlink(imagePath).catch(() => {});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Erro ao processar OCR na página ${i + 1}:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await worker.terminate();
|
||||||
|
|
||||||
|
const averageConfidence = pageResults.length > 0
|
||||||
|
? totalConfidence / pageResults.length
|
||||||
|
: 0;
|
||||||
|
|
||||||
|
return {
|
||||||
|
text: allText.trim(),
|
||||||
|
confidence: averageConfidence,
|
||||||
|
pages: pageResults,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao realizar OCR:', error);
|
||||||
|
throw new Error(`Falha ao realizar OCR: ${error instanceof Error ? error.message : 'Erro desconhecido'}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Limpa arquivos temporários antigos
|
||||||
|
*/
|
||||||
|
static async cleanupOldFiles(olderThanHours: number = 24): Promise<void> {
|
||||||
|
try {
|
||||||
|
const dirs = [this.TEMP_DIR, this.OUTPUT_DIR];
|
||||||
|
const now = Date.now();
|
||||||
|
const maxAge = olderThanHours * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
for (const dir of dirs) {
|
||||||
|
try {
|
||||||
|
const files = await fs.readdir(dir);
|
||||||
|
|
||||||
|
for (const file of files) {
|
||||||
|
const filePath = path.join(dir, file);
|
||||||
|
const stats = await fs.stat(filePath);
|
||||||
|
|
||||||
|
if (now - stats.mtimeMs > maxAge) {
|
||||||
|
await fs.unlink(filePath);
|
||||||
|
console.log(`Arquivo removido: ${filePath}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Erro ao limpar diretório ${dir}:`, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao limpar arquivos antigos:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Obtém informações sobre um PDF
|
||||||
|
*/
|
||||||
|
static async getPDFInfo(filePath: string): Promise<{
|
||||||
|
pages: number;
|
||||||
|
size: number;
|
||||||
|
title?: string;
|
||||||
|
author?: string;
|
||||||
|
creator?: string;
|
||||||
|
producer?: string;
|
||||||
|
creationDate?: Date;
|
||||||
|
modificationDate?: Date;
|
||||||
|
}> {
|
||||||
|
try {
|
||||||
|
const pdfBytes = await fs.readFile(filePath);
|
||||||
|
const pdfDoc = await PDFDocument.load(pdfBytes);
|
||||||
|
|
||||||
|
return {
|
||||||
|
pages: pdfDoc.getPageCount(),
|
||||||
|
size: pdfBytes.length,
|
||||||
|
title: pdfDoc.getTitle(),
|
||||||
|
author: pdfDoc.getAuthor(),
|
||||||
|
creator: pdfDoc.getCreator(),
|
||||||
|
producer: pdfDoc.getProducer(),
|
||||||
|
creationDate: pdfDoc.getCreationDate(),
|
||||||
|
modificationDate: pdfDoc.getModificationDate(),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erro ao obter informações do PDF:', error);
|
||||||
|
throw new Error(`Falha ao obter informações do PDF: ${error instanceof Error ? error.message : 'Erro desconhecido'}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user