Java pdfbox 프레임워크를 사용하여 HTML 또는 이미지에서 PDF를 만드는 방법

Java pdfbox 프레임워크를 사용하여 HTML 또는 이미지에서 PDF를 만드는 방법

다음 예제 코드는 다음과 같습니다.

In pom.xml insert or get the jar files and add
them to the java project if you don't use maven:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<!-- https://mvnrepository.com/artifact/org.
apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.hkirk/
java-html2image -->
<dependency>
<groupId>com.github.hkirk</groupId>
<artifactId>java-html2image</artifactId>
<version>0.9</version>
</dependency>
and Java code example here:
public static void main(String[] args) {
String html = "<html lang=\"en\"
xmlns=\http://www.w3.org/1999/xhtml\
style=\"cursor: default;\">"
+ "<body id=\"body\"
class=\"modal-open\"
style=\"cursor: default;
padding-right: 15px;\">\r\n"
+ "<p>" + "Hello World</p>";
+ "</body>\r\n" + "</html>\r\n"
+ "\r\n" + "";
HtmlImageGenerator imageGenerator =
new HtmlImageGenerator();
imageGenerator.loadHtml(html);
BufferedImage bimg = imageGenerator.getBufferedImage();
float width = bimg.getWidth();
float height = bimg.getHeight();
PDPage page = new PDPage(new PDRectangle(width, height));
PDDocument document = new PDDocument();
document.addPage(page);
try {
PDImageXObject pdImage =
LosslessFactory.createFromImage(document, bimg);
try (PDPageContentStream contentStream =
new PDPageContentStream(document, page,
AppendMode.OVERWRITE, true,true)) {
contentStream.drawImage(pdImage, 0, 0,
width, height);
document.save("xx.pdf");
document.close();
} catch (IOException e) {
//
} catch (Exception e) {
//
}
}

답장을 남겨주세요

다음과 같이 설계되었습니다. 워드프레스 | 개인 ì •ë³´ 보호 ì •ì±… | 나를 위한 | 블로그 지원 | “엔젤 가디언” 재단 | 바르나 기독교 온라인 라디오

© 2026 벨리슬라프 크라스테프. 모든 권리 보유.

WordPress로 디자인