Added imgscalr-lib
This commit is contained in:
parent
947f390708
commit
1b4f1c1e87
12
pom.xml
12
pom.xml
|
@ -118,6 +118,18 @@
|
|||
<type>pom</type>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency> -->
|
||||
<!-- <groupId>net.coobird</groupId> -->
|
||||
<!-- <artifactId>thumbnailator</artifactId> -->
|
||||
<!-- <version>[0.4, 0.5)</version> -->
|
||||
<!-- </dependency> -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.imgscalr</groupId>
|
||||
<artifactId>imgscalr-lib</artifactId>
|
||||
<version>4.2</version>
|
||||
</dependency>
|
||||
|
||||
<!-- LOGGER -->
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
|
||||
import java.awt.Color;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
|
@ -11,6 +13,8 @@ import java.util.Map;
|
|||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.gcube.application.geoportal.common.model.document.Project;
|
||||
import org.gcube.application.geoportalcommon.ConvertToDataValueObjectModel;
|
||||
import org.gcube.application.geoportalcommon.ProjectDVBuilder;
|
||||
|
@ -28,6 +32,8 @@ import org.gcube.application.geoportaldatamapper.Geoportal_JSON_Mapper;
|
|||
import org.gcube.application.geoportaldatamapper.URLParserUtil;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.imgscalr.Scalr;
|
||||
import org.imgscalr.Scalr.Method;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -36,8 +42,6 @@ import com.google.gson.JsonElement;
|
|||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.itextpdf.io.font.constants.StandardFonts;
|
||||
import com.itextpdf.io.font.otf.Glyph;
|
||||
import com.itextpdf.io.font.otf.GlyphLine;
|
||||
import com.itextpdf.io.image.ImageData;
|
||||
import com.itextpdf.io.image.ImageDataFactory;
|
||||
import com.itextpdf.kernel.colors.ColorConstants;
|
||||
|
@ -49,10 +53,7 @@ import com.itextpdf.kernel.font.PdfFont;
|
|||
import com.itextpdf.kernel.font.PdfFontFactory;
|
||||
import com.itextpdf.kernel.geom.Rectangle;
|
||||
import com.itextpdf.kernel.pdf.PdfAnnotationBorder;
|
||||
import com.itextpdf.kernel.pdf.PdfDictionary;
|
||||
import com.itextpdf.kernel.pdf.PdfDocument;
|
||||
import com.itextpdf.kernel.pdf.PdfOutputStream;
|
||||
import com.itextpdf.kernel.pdf.PdfString;
|
||||
import com.itextpdf.kernel.pdf.PdfWriter;
|
||||
import com.itextpdf.kernel.pdf.action.PdfAction;
|
||||
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
|
||||
|
@ -93,9 +94,11 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
|
||||
// private static String PROFILE_ID = "concessioni-estere";
|
||||
private static String PROFILE_ID = "profiledConcessioni";
|
||||
private static String PROJECT_ID = "6384db0c308f5c28c5ee0a32"; // 63d011c4dcac4551b9a6b930
|
||||
private static String PROJECT_ID = "6384ae78308f5c28c5ee08aa"; // "6384ae78308f5c28c5ee08aa"; //PRE
|
||||
// 63d011c4dcac4551b9a6b930
|
||||
// //6442653ac6a0422d9e9042e0
|
||||
// //6399de3ca0a4545420373251
|
||||
//645a152fd7fb4a463302240d
|
||||
|
||||
// IMPORTANT!!!! #NB SET USERNAME = null to test PUBLIC ACCESS
|
||||
|
||||
|
@ -171,7 +174,7 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
Document document = new Document(pdf);
|
||||
|
||||
// Adding logo D4GNA
|
||||
Image image = toImageFromLink(logoD4GNA, 70);
|
||||
Image image = toImageFromLink(logoD4GNA, 70, false);
|
||||
// image.setMaxHeight(150);
|
||||
image.setFixedPosition(document.getLeftMargin(),
|
||||
pdf.getDefaultPageSize().getHeight() - image.getImageHeight() + 90);
|
||||
|
@ -220,11 +223,11 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
LocalDateTime now = LocalDateTime.now();
|
||||
PdfFont font = PdfFontFactory.createFont(StandardFonts.COURIER_OBLIQUE);
|
||||
Style style2 = new Style().setFont(font).setFontSize(8);
|
||||
Paragraph pt = new Paragraph().add("Created at "+now.format(dtf)).addStyle(style2);
|
||||
Paragraph pt = new Paragraph().add("Created at " + now.format(dtf)).addStyle(style2);
|
||||
// pt.setFixedPosition(document.getLeftMargin() + 30, 50,
|
||||
// document.getPdfDocument().getDefaultPageSize().getWidth() - 75);
|
||||
|
||||
pt.setMarginTop(30);
|
||||
|
||||
pt.setMarginTop(50);
|
||||
document.add(pt);
|
||||
|
||||
// Credits
|
||||
|
@ -248,7 +251,7 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
tableCredits.setFixedPosition(document.getLeftMargin() + 30, 15,
|
||||
document.getPdfDocument().getDefaultPageSize().getWidth() - 75);
|
||||
// Adding logo D4GNA
|
||||
Image imageLogo = toImageFromLink(logoD4GNA, 40);
|
||||
Image imageLogo = toImageFromLink(logoD4GNA, 40, false);
|
||||
Cell cell1 = new Cell();
|
||||
cell1.setBorder(Border.NO_BORDER);
|
||||
cell1.add(imageLogo);
|
||||
|
@ -263,7 +266,7 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
tableCredits.addCell(cell2);
|
||||
|
||||
// Adding logo Powered By D4Science
|
||||
Image poweredByLink = toImageFromLink(logoPoweredByD4Science, 80);
|
||||
Image poweredByLink = toImageFromLink(logoPoweredByD4Science, 80, false);
|
||||
Cell cell3 = new Cell();
|
||||
cell3.setBorder(Border.NO_BORDER);
|
||||
cell3.add(poweredByLink);
|
||||
|
@ -329,7 +332,7 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
if (files != null) {
|
||||
for (FilesetDV fileset : files) {
|
||||
// Paragraph paragraphInt = new Paragraph(fileset.getGcubeProfileFieldName());
|
||||
List<Image> listLinks = toImages(fileset.getListPayload());
|
||||
List<Image> listLinks = toImages(fileset.getListPayload(), 250, false);
|
||||
for (Image image : listLinks) {
|
||||
image.setBorder(Border.NO_BORDER);
|
||||
tableI.addCell(image);
|
||||
|
@ -366,8 +369,8 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
tableI.setTextAlignment(TextAlignment.CENTER);
|
||||
String wmsLink = gCubeLayer.getWMSLink();
|
||||
System.out.println("wmsLink: " + wmsLink);
|
||||
wmsLink = URLParserUtil.setValueOfParameter("format", wmsLink, "image/png", true);
|
||||
Image image = toImageFromLink(wmsLink, 250);
|
||||
wmsLink = URLParserUtil.setValueOfParameter("format", wmsLink, "image/jpeg", true);
|
||||
Image image = toImageFromLink(wmsLink, 250, false);
|
||||
tableI.setTextAlignment(TextAlignment.CENTER);
|
||||
tableI.setHorizontalAlignment(HorizontalAlignment.CENTER);
|
||||
tableI.addCell(image);
|
||||
|
@ -431,12 +434,13 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
return table;
|
||||
}
|
||||
|
||||
private static List<Image> toImages(List<PayloadDV> payloads) throws IOException {
|
||||
private static List<Image> toImages(List<PayloadDV> payloads, Integer maxWidth, boolean scaleImg)
|
||||
throws IOException {
|
||||
|
||||
List<Image> listImages = new ArrayList<Image>(payloads.size());
|
||||
for (PayloadDV payloadDV : payloads) {
|
||||
|
||||
Image img = toImageFromLink(payloadDV.getLink(), 250);
|
||||
Image img = toImageFromLink(payloadDV.getLink(), maxWidth, scaleImg);
|
||||
if (img != null)
|
||||
listImages.add(img);
|
||||
|
||||
|
@ -446,25 +450,44 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
|
||||
}
|
||||
|
||||
private static Image toImageFromLink(String link, Integer maxWidth) {
|
||||
private static Image toImageFromLink(String link, Integer maxWidth, boolean scaleImg) {
|
||||
Image img = null;
|
||||
try {
|
||||
ImageData data = ImageDataFactory.create(link);
|
||||
|
||||
ImageData data = null;
|
||||
if (scaleImg) {
|
||||
final InputStream is = new URL(link).openStream();
|
||||
BufferedImage inputImage = ImageIO.read(is);
|
||||
BufferedImage out = simpleResizeImage(inputImage, maxWidth);
|
||||
data = ImageDataFactory.create(out, Color.WHITE);
|
||||
is.close();
|
||||
} else {
|
||||
data = ImageDataFactory.create(new URL(link));
|
||||
}
|
||||
|
||||
img = new Image(data);
|
||||
// img.setBorder(Border.NO_BORDER);
|
||||
|
||||
if (maxWidth != null)
|
||||
img.setMaxWidth(maxWidth);
|
||||
else
|
||||
img.setAutoScaleWidth(true);
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
} catch (Exception e) {
|
||||
// silent
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
return img;
|
||||
}
|
||||
|
||||
private static BufferedImage simpleResizeImage(BufferedImage originalImage, int targetWidth) throws Exception {
|
||||
if (targetWidth > 0) {
|
||||
return Scalr.resize(originalImage, Method.QUALITY, targetWidth);
|
||||
}
|
||||
return originalImage;
|
||||
}
|
||||
|
||||
private static List<Link> toPDFLinks(List<PayloadDV> payloads, Integer marginLeft) throws IOException {
|
||||
|
||||
List<Link> listLinks = new ArrayList<Link>(payloads.size());
|
||||
|
@ -492,59 +515,6 @@ public class Geoportal_Export_To_PDF_Tests {
|
|||
return theLink;
|
||||
}
|
||||
|
||||
protected class TextFooterEventHandler implements IEventHandler {
|
||||
|
||||
protected Document doc;
|
||||
|
||||
public TextFooterEventHandler(Document doc) {
|
||||
|
||||
this.doc = doc;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
public void handleEvent(Event event) {
|
||||
|
||||
PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
|
||||
|
||||
PdfCanvas canvas = new PdfCanvas(docEvent.getPage());
|
||||
|
||||
Rectangle pageSize = docEvent.getPage().getPageSize();
|
||||
|
||||
canvas.beginText();
|
||||
|
||||
try {
|
||||
|
||||
canvas.setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA_OBLIQUE), 5);
|
||||
|
||||
} catch (IOException e) {
|
||||
|
||||
e.printStackTrace();
|
||||
|
||||
}
|
||||
|
||||
canvas.moveText(
|
||||
(pageSize.getRight() - doc.getRightMargin() - (pageSize.getLeft() + doc.getLeftMargin())) / 2
|
||||
+ doc.getLeftMargin(),
|
||||
pageSize.getTop() - doc.getTopMargin() + 10)
|
||||
|
||||
.showText("this is a header")
|
||||
|
||||
.moveText(0,
|
||||
(pageSize.getBottom() + doc.getBottomMargin()) - (pageSize.getTop() + doc.getTopMargin())
|
||||
- 20)
|
||||
|
||||
.showText("this is a footer")
|
||||
|
||||
.endText()
|
||||
|
||||
.release();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* convert json Data to structured Html text
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue