Closes #118: create pdf from text plain files

This commit is contained in:
Benjamin Gamard 2017-07-31 01:51:23 +02:00
parent 330de495db
commit e0214a6a9f
4 changed files with 64 additions and 55 deletions

View File

@ -1,26 +1,24 @@
package com.sismics.docs.core.util; package com.sismics.docs.core.util;
import java.awt.image.BufferedImage; import com.sismics.docs.core.model.jpa.File;
import java.io.IOException; import com.sismics.tess4j.Tesseract;
import java.io.InputStream; import com.sismics.util.ImageUtil;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.imageio.ImageIO;
import org.imgscalr.Scalr; import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Method; import org.imgscalr.Scalr.Method;
import org.imgscalr.Scalr.Mode; import org.imgscalr.Scalr.Mode;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.sismics.docs.core.model.jpa.File; import javax.crypto.Cipher;
import com.sismics.tess4j.Tesseract; import javax.crypto.CipherInputStream;
import com.sismics.util.ImageUtil; import javax.crypto.CipherOutputStream;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
/** /**
* File entity utilities. * File entity utilities.

View File

@ -1,18 +1,16 @@
package com.sismics.docs.core.util; package com.sismics.docs.core.util;
import java.awt.image.BufferedImage; import com.google.common.base.Charsets;
import java.io.ByteArrayInputStream; import com.google.common.base.Strings;
import java.io.ByteArrayOutputStream; import com.google.common.io.CharStreams;
import java.io.IOException; import com.google.common.io.Closer;
import java.io.InputStream; import com.lowagie.text.*;
import java.nio.file.Files; import com.lowagie.text.pdf.PdfWriter;
import java.nio.file.Path; import com.sismics.docs.core.dao.jpa.dto.DocumentDto;
import java.text.SimpleDateFormat; import com.sismics.docs.core.model.jpa.File;
import java.util.Date; import com.sismics.docs.core.util.pdf.PdfPage;
import java.util.List; import com.sismics.util.ImageUtil;
import com.sismics.util.mime.MimeType;
import javax.imageio.ImageIO;
import org.apache.pdfbox.io.MemoryUsageSetting; import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDDocument;
@ -32,13 +30,14 @@ import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.common.base.Strings; import javax.imageio.ImageIO;
import com.google.common.io.Closer; import java.awt.image.BufferedImage;
import com.sismics.docs.core.dao.jpa.dto.DocumentDto; import java.io.*;
import com.sismics.docs.core.model.jpa.File; import java.nio.file.Files;
import com.sismics.docs.core.util.pdf.PdfPage; import java.nio.file.Path;
import com.sismics.util.ImageUtil; import java.text.SimpleDateFormat;
import com.sismics.util.mime.MimeType; import java.util.Date;
import java.util.List;
/** /**
* PDF utilities. * PDF utilities.
@ -120,8 +119,20 @@ public class PdfUtil {
if (reset) { if (reset) {
inputStream.reset(); inputStream.reset();
} }
// TODO Create a PDF from the text plain
return null; Document output = new Document(PageSize.A4, 40, 40, 40, 40);
ByteArrayOutputStream pdfOutputStream = new ByteArrayOutputStream();
PdfWriter.getInstance(output, pdfOutputStream);
output.open();
String content = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
Font font = FontFactory.getFont(FontFactory.COURIER);
Paragraph paragraph = new Paragraph(content, font);
paragraph.setAlignment(Element.ALIGN_LEFT);
output.add(paragraph);
output.close();
return new ByteArrayInputStream(pdfOutputStream.toByteArray());
} }
/** /**

View File

@ -1,7 +1,17 @@
package com.sismics.docs.rest; package com.sismics.docs.rest;
import java.io.InputStream; import com.google.common.io.ByteStreams;
import java.util.Date; import com.google.common.io.Resources;
import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.util.filter.TokenBasedSecurityFilter;
import com.sismics.util.mime.MimeType;
import com.sismics.util.mime.MimeTypeUtil;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
import org.joda.time.format.DateTimeFormat;
import org.junit.Assert;
import org.junit.Test;
import javax.json.JsonArray; import javax.json.JsonArray;
import javax.json.JsonObject; import javax.json.JsonObject;
@ -10,20 +20,8 @@ import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.Response.Status;
import java.io.InputStream;
import org.glassfish.jersey.media.multipart.FormDataMultiPart; import java.util.Date;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
import org.joda.time.format.DateTimeFormat;
import org.junit.Assert;
import org.junit.Test;
import com.google.common.io.ByteStreams;
import com.google.common.io.Resources;
import com.sismics.docs.core.util.DirectoryUtil;
import com.sismics.util.filter.TokenBasedSecurityFilter;
import com.sismics.util.mime.MimeType;
import com.sismics.util.mime.MimeTypeUtil;
/** /**
* Exhaustive test of the document resource. * Exhaustive test of the document resource.
@ -569,7 +567,7 @@ public class TestDocumentResource extends BaseJerseyTest {
String document1Id = json.getString("id"); String document1Id = json.getString("id");
Assert.assertNotNull(document1Id); Assert.assertNotNull(document1Id);
// Add a PDF file // Add a plain text file
String file1Id; String file1Id;
try (InputStream is = Resources.getResource("file/document.txt").openStream()) { try (InputStream is = Resources.getResource("file/document.txt").openStream()) {
StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "document.txt"); StreamDataBodyPart streamDataBodyPart = new StreamDataBodyPart("file", is, "document.txt");

View File

@ -1,2 +1,4 @@
This is a test document This is a test document
Please love me Please love me
&é"'(-è_çà)=$^ù*
조선글