From 443935b1a3a9002d87f586da5f8fd34db054c3ab Mon Sep 17 00:00:00 2001 From: V1centR Date: Mon, 12 Mar 2018 08:54:53 -0300 Subject: [PATCH] apply psr --- .classpath | 6 +++ .gitignore | 3 ++ .project | 17 ++++++++ PDFConverter.java | 98 +++++++++++++++++++++++++++++++---------------- 4 files changed, 91 insertions(+), 33 deletions(-) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3f3893a --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84136e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/ConversorPDF.class +/PDFConverter.class + diff --git a/.project b/.project new file mode 100644 index 0000000..f093974 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + quia-java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/PDFConverter.java b/PDFConverter.java index c7fb195..b72df15 100644 --- a/PDFConverter.java +++ b/PDFConverter.java @@ -1,34 +1,66 @@ -public class ConversorPDF { - - // A aplicação foi projetada para tratar arquivos WORD, temos que cuidar para que - // alterações não mudem o comportamento, para que não exista reflexo para clientes - // legados - public String tipoDocumento = "WORD"; - - /** - * Esse método recebe o como entrada o arquivo que vai ser convertido - * para PDF - * - * @param bytesArquivo - * @return - */ - public byte[] converteParaPDF(byte[] bytesArquivo){ - if(tipoDocumento.equals("WORD")) { - InputStream entrada = new ByteArrayInputStream(bytesArquivo); - com.aspose.words.Document documentoWord = new com.aspose.words.Document(entrada); - ByteArrayOutputStream documentoPDF = new ByteArrayOutputStream(); - documentoWord.save(documentoPDF, SaveFormat.PDF); - - return documentoPDF.toByteArray(); - } else { - InputStream entrada = new ByteArrayInputStream(bytesArquivo); - Workbook workbook = new Workbook(entrada); - PdfSaveOptions opcaoSalvar = new PdfSaveOptions(); - opcaoSalvar.setCompliance(PdfCompliance.PDF_A_1_B); - ByteArrayOutputStream documentoPDF = new ByteArrayOutputStream(); - workbook.save(documentoPDF, opcaoSalvar); - - return documentoPDF.toByteArray(); - } - } +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +import com.aspose.cells.*; +import com.aspose.words.PdfCompliance; +import com.aspose.words.PdfSaveOptions; +import com.aspose.words.SaveFormat; + +public class PDFConverter { + + public String tipoDocumento = ""; + + /** + * Método recebe o como entrada o arquivo que vai ser convertido para PDF + * + * @param bytesArquivo + * @return + */ + public byte[] converteParaPDF(byte[] bytesArquivo) { + + if (tipoDocumento.equals("EXCEL")) { + + return workFile2Excel(bytesArquivo); + + } + + if (tipoDocumento.equals("WORD")) { + + return workFile(bytesArquivo); + + } else { + + InputStream entrada = new ByteArrayInputStream(bytesArquivo); + Workbook workbook = new Workbook(entrada); + PdfSaveOptions opcaoSalvar = new PdfSaveOptions(); + opcaoSalvar.setCompliance(PdfCompliance.PDF_A_1_B); + ByteArrayOutputStream documentoPDF = new ByteArrayOutputStream(); + workbook.save(documentoPDF, opcaoSalvar); + + return documentoPDF.toByteArray(); + } + + } + + private byte[] workFile(byte[] bytesArquivo) throws Exception { + + InputStream entrada = new ByteArrayInputStream(bytesArquivo); + com.aspose.words.Document documentoWord = new com.aspose.words.Document(entrada); + ByteArrayOutputStream documentoPDF = new ByteArrayOutputStream(); + documentoWord.save(documentoPDF, SaveFormat.PDF); + + return documentoPDF.toByteArray(); + + } + + private byte[] workFile2Excel(byte[] bytesArquivo) throws Exception { + + InputStream entrada = new ByteArrayInputStream(bytesArquivo); + Workbook asposeCellWb = new Workbook(entrada); + ByteArrayOutputStream documentoPDF = new ByteArrayOutputStream(); + asposeCellWb.save(documentoPDF, SaveFormat.PDF); + + return documentoPDF.toByteArray(); + } }