HtmlExporterUtils { public static byte[] image2pdf(byte[] image, Rectangle pageSize, Float marginLeft, Float marginRight, Float marginTop, Float marginBottom) { Document document = new Document(pageSize == null ? PageSize.A4 : pageSize, marginLeft == null ? 0f : marginLeft, marginRight == null ? 0f : marginRight, marginTop == null ? 0f : marginTop, marginBottom == null ? 0f : marginBottom); PdfWriter pdfWriter; try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { pdfWriter = PdfWriter.getInstance(document, baos); document.open(); document.add(Image.getInstance(image, true)); document.close(); pdfWriter.close(); return baos.toByteArray(); } catch (DocumentException | IOException e) { throw new RuntimeException(e); } } private  HtmlExporterUtils(); static void byte2File(byte[] bytes, File file); static byte[] image2pdfA4(byte[] image); static byte[] image2pdf(byte[] image, Rectangle pageSize, Float marginLeft, Float marginRight,
                                   Float marginTop, Float marginBottom); static PhantomJSDriver prepare(String url, Cookie addedCookie, Integer width, Integer height); static Rectangle getSizeFromImage(File image); static Rectangle getSizeFromImage(InputStream inputStream); static Rectangle getSizeFromImage(byte[] bytes); static Rectangle getSizeFromImage(String base64); static void release(PhantomJSDriver driver);  }
@Ignore @Test public void image2pdfTest() { String url = "http: HtmlExporter2BYTES htmlExporter2BYTES = new HtmlExporter2BYTES(); byte[] bytes = htmlExporter2BYTES.convert2Image(url, null, null); HtmlExporterUtils.byte2File(HtmlExporterUtils.image2pdf( bytes, HtmlExporterUtils.getSizeFromImage(new ByteArrayInputStream(bytes)), 0f, 0f, 0f, 0f), new File(RESOURCES_PATH.concat(PREFIX_OUTPUT_PATH).concat("login.pdf"))); }