Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
*
* @param html
* @param baseDir
* @return
* @throws IOException
*/
public static LayoutInfo layoutThenGetInfo(String html, File baseDir) throws IOException {
LayoutInfo ret;
PdfRendererBuilder builderTemp = new PdfRendererBuilder();
useFonts(builderTemp, null);
builderTemp.withHtmlContent(html, buildBaseDocumentUri1(baseDir));
builderTemp.useFastMode();
try (PdfBoxRenderer renderer = builderTemp.buildPdfRenderer(); PDDocument doc = renderer.getPdfDocument();) {//need to close doc if use box
renderer.layout();
// The root box is , the first child is , then
.
Box box = renderer.getRootBox();//1mm=76; 2mm=151;
List pageList = box.getLayer().getPages();//1mm=215p; 2mm=110p;
ret = new LayoutInfo(pageList.size(), box.getWidth(), box.getHeight());
}
return ret;
}
public static byte[] html2PDF(String html, File baseDir, ProtectionPolicy protectionPolicy, PDDocumentInformation info, float pdfVersion) throws IOException {
PdfRendererBuilder builder = new PdfRendererBuilder();
useFonts(builder, null);
builder.withHtmlContent(html, buildBaseDocumentUri1(baseDir));
if (info != null) {
builder.withProducer(info.getProducer());
}
builder.useFastMode();
//builder.useDefaultPageSize(pageWidth, pageHeight, units);
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
builder.toStream(baos);
try (PdfBoxRenderer renderer = builder.buildPdfRenderer(); PDDocument doc = renderer.getPdfDocument();) {
//security
if (protectionPolicy == null) {
protectionPolicy = buildStandardProtectionPolicy(null, null);
}
doc.protect(protectionPolicy);
doc.setVersion(pdfVersion);
// //info
// if (info != null) {
// doc.setDocumentInformation(info);
// }
//build PDF
renderer.layout();//com.openhtmltopdf.load INFO:: Loading font(ArialUnicodeMS) from PDFont supplier now.
renderer.createPDF();//com.openhtmltopdf.general INFO:: Using fast-mode renderer. Prepare to fly.
}
return baos.toByteArray();
}
}
protected static String buildBaseDocumentUri1(File baseDirectory) throws IOException {
try {
//return new File(baseDirectory, htmlFileName).toURI().toURL().toExternalForm();
return baseDirectory.toURI().toURL().toExternalForm();
} catch (MalformedURLException ex) {
throw new IOException("Invalid baseDirectory=" + baseDirectory, ex);
}
}
/**
* @param pdfData
* @param dpi
* @param formatName a {@code String} containing the informal name of a
* format (e.g., "jpeg", "png" or "tiff".
* @param destination
* @return
* @throws IOException
*/
public static List pdf2Images(byte[] pdfData, float dpi, String formatName, RenderDestination destination) throws IOException {
return pdf2Images(pdfData, dpi, ImageType.RGB, formatName, destination);
}
/**
* @param pdfData
* @param dpi
* @param imageType
* @param formatName a {@code String} containing the informal name of a
* format (e.g., "jpeg", "png" or "tiff".
* @param destination
* @return
* @throws IOException
*/
public static List pdf2Images(byte[] pdfData, float dpi, ImageType imageType, String formatName, RenderDestination destination) throws IOException {
List images = pdf2Images(pdfData, dpi, imageType, destination);
List imageDatas = images2Bytes(images, formatName);
return imageDatas;
}
/**
* @param pdfFile
* @param dpi
* @param imageType
* @param formatName a {@code String} containing the informal name of a
* format (e.g., "jpeg", "png" or "tiff".
* @param destination
* @return
* @throws IOException
*/
public static List pdf2Images(File pdfFile, float dpi, ImageType imageType, String formatName, RenderDestination destination) throws IOException {
List images = pdf2Images(pdfFile, dpi, imageType, destination);
List imageDatas = images2Bytes(images, formatName);
return imageDatas;
}
public static List pdf2Images(byte[] pdfData, float dpi, ImageType imageType, RenderDestination destination) throws IOException {
//1: Loading an Existing PDF Document
try (PDDocument document = PDDocument.load(pdfData);) {
return pdf2Images(document, dpi, imageType, destination);
}
}
public static List pdf2Images(File pdfFile, float dpi, ImageType imageType, RenderDestination destination) throws IOException {
//1: Loading an Existing PDF Document
try (PDDocument document = PDDocument.load(pdfFile);) {
return pdf2Images(document, dpi, imageType, destination);
}
}
/**
* @param document make sure the caller will close the document
* @param dpi 300
* @param imageType
* @param destination
* @return
* @throws IOException
*/
public static List pdf2Images(PDDocument document, float dpi, ImageType imageType, RenderDestination destination) throws IOException {
PDFRenderer renderer = new PDFRenderer(document);
int totalPages = document.getNumberOfPages();
List images = new ArrayList();
for (int currentPage = 0; currentPage < totalPages; currentPage++) {
BufferedImage image = renderer.renderImage(currentPage, dpi / 72f, imageType, destination);
images.add(image);
}
return images;
}
/**
* @param images
* @param formatName a {@code String} containing the informal name of a
* format (e.g., "jpeg", "png" or "tiff".
* @return
* @throws IOException
*/
public static List images2Bytes(List images, String formatName) throws IOException {
List imageDataList = new ArrayList(images.size());
for (BufferedImage image : images) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
ImageIO.write(image, formatName, baos);
byte[] imageData = baos.toByteArray();
imageDataList.add(imageData);
}
}
return imageDataList;
}
public static interface Writer {
void write(PDDocument doc, T dto) throws IOException;
}
public static byte[] writePDF(Writer writer, Object dto, float pdfVersion) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); PDDocument doc = new PDDocument()) {
useFonts(null, doc);
doc.protect(buildStandardProtectionPolicy(null, null));
doc.setVersion(pdfVersion);
writer.write(doc, dto);
doc.save(baos);
return baos.toByteArray();
}
}
}