From 4eab947430a89a3fb75fe4b8d667fcdc745c90c2 Mon Sep 17 00:00:00 2001 From: Sylvain Jermini Date: Tue, 28 Apr 2020 13:41:41 +0200 Subject: [PATCH] better handling of href uri #469 --- .../pdfboxout/PdfBoxFastLinkManager.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxFastLinkManager.java b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxFastLinkManager.java index 8f753bcf6..318782406 100644 --- a/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxFastLinkManager.java +++ b/openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxFastLinkManager.java @@ -29,6 +29,7 @@ import java.awt.*; import java.awt.geom.*; import java.io.IOException; +import java.net.URI; import java.util.*; import java.util.List; import java.util.Map.Entry; @@ -232,7 +233,7 @@ private void addUriAsLink(RenderingContext c, Box box, PDPage page, float pageHe } else { XRLog.general(Level.WARNING, "Could not find valid target for link. Link href = " + uri); } - } else if (uri.contains("://")) { + } else if (isURI(uri)) { PDActionURI uriAct = new PDActionURI(); uriAct.setURI(uri); @@ -249,6 +250,15 @@ private void addUriAsLink(RenderingContext c, Box box, PDPage page, float pageHe } } + private static boolean isURI(String uri) { + try { + return URI.create(uri) != null; + } catch (IllegalArgumentException e) { + XRLog.general(Level.INFO, "'"+uri+"' in href is not a valid URI, will be skipped"); + return false; + } + } + @SuppressWarnings("BooleanMethodIsAlwaysInverted") private boolean placeAnnotation(AffineTransform transform, Shape linkShape, Rectangle2D targetArea, PDAnnotationLink annot) {