자바로 PDF 파일을 만드는 방법

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 라이브러리를 사용하면 Java에서 PDF 파일을 간단하게 생성할 수 있습니다. 이 라이브러리는 HTML 문자열용 renderHtmlAsPdf(), HTML 파일용 renderHtmlFileAsPdf(), 웹 페이지용 renderUrlAsPdf()와 같은 메서드를 통해 HTML을 PDF로 변환합니다.

빠른 시작: Java로 첫 번째 PDF 만들기

  1. pom.xml 파일에 IronPDF 종속성을 추가하세요.

    <dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>1.0.0</version>
    </dependency>
    <dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>1.0.0</version>
    </dependency>
    XML
  2. IronPDF 클래스 가져오기:

    import com.ironsoftware.ironpdf.*;
    import com.ironsoftware.ironpdf.*;
    JAVA
  3. HTML에서 PDF 생성: ```java :title=빠른 시작 PdfDocument pdf = PdfDocument.renderHtmlAsPdf(""); pdf.saveAs(Paths.get("output.pdf"));

자바를 이용한 프로그래밍 방식으로 PDF를 생성하면 송장, 보고서 및 기타 비즈니스 문서를 필요에 따라 자동으로 생성할 수 있습니다.

이 가이드는 Java 애플리케이션에서 IronPDF를 사용하여 PDF 파일을 프로그래밍 방식으로 생성하는 방법을 다룹니다.

IronPDF 자바 PDF 라이브러리란 무엇인가요?

IronPDF는 HTML에서 PDF 문서를 생성하는 Java 라이브러리입니다. 이 프로그램은 PDF를 생성하고 사용자 정의하는 기능을 제공하며, 다음과 같은 기능을 포함합니다.

  1. 텍스트, 이미지 및 기타 콘텐츠 유형 추가
  2. 글꼴, 색상 선택, 레이아웃 및 서식 제어

IronPDF는 .NET Framework를 기반으로 구축되었으므로 .NET 및 Java 애플리케이션 모두에서 사용할 수 있습니다. 이 라이브러리는 사용자 지정 워터마크 , PDF 압축양식 생성 과 같은 고급 기능을 지원합니다.

IronPDF는 파일 형식 변환, 텍스트 및 데이터 추출, 암호 암호화 등 PDF 관련 작업도 처리합니다. 필요에 따라 여러 PDF 파일을 병합 하거나 분할 할 수 있습니다.

Java 애플리케이션에서 PDF 문서를 생성하는 방법은 무엇인가요?

필요한 사전 조건은 무엇인가요?

Maven 프로젝트에서 IronPDF를 사용하려면 다음 필수 구성 요소가 설치되어 있는지 확인하십시오.

  1. 자바 개발 키트(JDK): 자바 애플리케이션을 컴파일하고 실행하는 데 필요합니다. Download from Oracle website.
  2. Maven: 프로젝트 라이브러리 다운로드에 필요합니다. Apache Maven 웹사이트 에서 다운로드하세요.
  3. IronPDF 라이브러리: pom.xml 파일에 Maven 프로젝트의 종속성으로 추가하세요.
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>com.ironsoftware</groupId>
    <artifactId>ironpdf</artifactId>
    <version>1.0.0</version>
</dependency>
XML

Gradle 프로젝트의 경우 다음 명령어를 사용하여 IronPDF를 추가하세요.

implementation 'com.ironsoftware:ironpdf:1.0.0'

코드를 작성하기 전에 어떤 단계를 거쳐야 할까요?

먼저, 다음 import 문을 Java 소스 파일에 추가하세요.

import com.ironsoftware.ironpdf.*;
import com.ironsoftware.ironpdf.*;
JAVA

특정 기능을 구현하려면 추가 클래스를 가져오세요.

import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.security.SecurityOptions;
import com.ironsoftware.ironpdf.security.SecurityManager;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.security.SecurityOptions;
import com.ironsoftware.ironpdf.security.SecurityManager;
JAVA

다음으로, main 방식을 사용하여 유효한 라이선스 키로 IronPDF를 구성하십시오.

License.setLicenseKey("Your license key");
License.setLicenseKey("Your license key");
JAVA

참고 : 라이선스 키를 사용하면 워터마크가 제거됩니다. 라이선스 키를 구매 하거나 무료 체험판을 이용해 보세요 . 라이선스가 없으면 PDF 파일에 워터마크가 생성됩니다. 자세한 내용은 라이선스 키 사용 설명서를 참조하세요.

자바에서 HTML 문자열을 PDF 파일로 만드는 방법은 무엇인가요?

HTML 문자열을 PDF로 변환하려면 renderHtmlAsPdf()를 사용하세요. 이 메서드는 HTML5, CSS3 및 JavaScript 렌더링을 지원합니다.

renderHtmlAsPdf에 HTML 문자열을 전달하세요. IronPDF는 이를 PdfDocument 인스턴스로 변환합니다.

// HTML content to be converted to PDF
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";

// Convert HTML string to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);

// Save the PDF document to a file
pdf.saveAs(Paths.get("html.pdf"));
// HTML content to be converted to PDF
String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";

// Convert HTML string to PDF
PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);

// Save the PDF document to a file
pdf.saveAs(Paths.get("html.pdf"));
JAVA

이렇게 하면 HTML 콘텐츠가 포함된 "html.pdf" 파일이 생성됩니다.

CSS 스타일링을 사용하여 복잡한 HTML을 포함시키세요:

// HTML with CSS styling
String styledHtml = """
    <!DOCTYPE html>
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; }
            h1 { color: #2563eb; }
            .content { margin: 20px; padding: 15px; background-color: #f3f4f6; }
        </style>
    </head>
    <body>
        <div class="content">
            <h1>Styled PDF Document</h1>
            <p>This PDF was created from HTML with custom CSS styling.</p>
        </div>
    </body>
    </html>
    """;

PdfDocument styledPdf = PdfDocument.renderHtmlAsPdf(styledHtml);
styledPdf.saveAs(Paths.get("styled.pdf"));
// HTML with CSS styling
String styledHtml = """
    <!DOCTYPE html>
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; }
            h1 { color: #2563eb; }
            .content { margin: 20px; padding: 15px; background-color: #f3f4f6; }
        </style>
    </head>
    <body>
        <div class="content">
            <h1>Styled PDF Document</h1>
            <p>This PDF was created from HTML with custom CSS styling.</p>
        </div>
    </body>
    </html>
    """;

PdfDocument styledPdf = PdfDocument.renderHtmlAsPdf(styledHtml);
styledPdf.saveAs(Paths.get("styled.pdf"));
JAVA

고급 변환에 대해서는 HTML을 PDF로 변환하는 튜토리얼을 참조하세요.

자바를 사용하여 HTML 페이지에서 PDF 파일을 생성하는 방법은 무엇인가요?

로컬 HTML 파일에서 PDF 생성:

// Convert HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
// Convert HTML file to PDF
PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");

// Save the PdfDocument to a file
myPdf.saveAs(Paths.get("html_file_saved.pdf"));
JAVA

renderHtmlFileAsPdf 메서드는 파일 경로를 문자열 또는 Path 객체로 받습니다.

IronPDF는 브라우저와 마찬가지로 CSS와 JavaScript를 사용하여 HTML 요소를 렌더링합니다. 여기에는 외부 CSS 파일, 이미지 및 자바스크립트 라이브러리가 포함됩니다. 자세한 내용은 HTML 파일을 PDF로 변환하는 방법을 참조하세요.

PDF 파일을 특정 위치에 저장하려면 saveAs를 사용하세요.

Java에서 URL을 이용해 PDF 파일을 생성하는 방법은 무엇인가요?

웹 페이지에서 PDF를 생성하려면 renderUrlAsPdf()를 사용하세요.

// Convert a URL to PDF
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
// Convert a URL to PDF
PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
JAVA

인증이 필요한 웹사이트의 경우 자격 증명을 제공하세요.

// Create render options with login credentials
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setAuthUsername("username");
renderOptions.setAuthPassword("password");

// Convert secured URL to PDF
PdfDocument securedPdf = PdfDocument.renderUrlAsPdf("https://secure-site.com", renderOptions);
securedPdf.saveAs(Paths.get("secured.pdf"));
// Create render options with login credentials
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setAuthUsername("username");
renderOptions.setAuthPassword("password");

// Convert secured URL to PDF
PdfDocument securedPdf = PdfDocument.renderUrlAsPdf("https://secure-site.com", renderOptions);
securedPdf.saveAs(Paths.get("secured.pdf"));
JAVA

자세한 내용은 PDF 코드 예제 URL을 참조하세요. 복잡한 인증 방식에 대해서는 웹사이트 및 시스템 로그인 페이지를 참조하세요.

PDF 파일 형식을 어떻게 지정하나요?

PDF 서식을 지정하려면 ChromePdfRenderOptions를 사용하십시오. 페이지 방향, 크기 및 여백을 구성합니다. 렌더링 메서드의 두 번째 인수로 옵션을 전달하세요.

// Create render options with custom settings
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();

// Set page orientation to landscape
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);

// Set custom paper size (Letter size)
renderOptions.setPaperSize(PaperSize.LETTER);

// Set custom margins (in millimeters)
renderOptions.setMarginTop(10);
renderOptions.setMarginRight(10);
renderOptions.setMarginBottom(10);
renderOptions.setMarginLeft(10);

// Enable background images and colors
renderOptions.setPrintHtmlBackgrounds(true);

// Apply options to PDF generation
PdfDocument formattedPdf = PdfDocument.renderHtmlAsPdf("<h1>Formatted PDF</h1>", renderOptions);
formattedPdf.saveAs(Paths.get("formatted.pdf"));
// Create render options with custom settings
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();

// Set page orientation to landscape
renderOptions.setPaperOrientation(PaperOrientation.LANDSCAPE);

// Set custom paper size (Letter size)
renderOptions.setPaperSize(PaperSize.LETTER);

// Set custom margins (in millimeters)
renderOptions.setMarginTop(10);
renderOptions.setMarginRight(10);
renderOptions.setMarginBottom(10);
renderOptions.setMarginLeft(10);

// Enable background images and colors
renderOptions.setPrintHtmlBackgrounds(true);

// Apply options to PDF generation
PdfDocument formattedPdf = PdfDocument.renderHtmlAsPdf("<h1>Formatted PDF</h1>", renderOptions);
formattedPdf.saveAs(Paths.get("formatted.pdf"));
JAVA

더 많은 옵션을 보려면 PDF 생성 설정을 참조하세요. 사용자 지정 용지 크기여백을 살펴보세요.

PDF 파일에 비밀번호를 설정하여 보호하는 방법은 무엇인가요?

PDF 파일에 비밀번호를 설정하여 보호하려면 SecurityOptions를 사용하세요.

// Create security options and set user password
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
// Create security options and set user password
SecurityOptions securityOptions = new SecurityOptions();
securityOptions.setUserPassword("shareable");
JAVA

고급 보안 옵션을 설정하세요:

// Advanced security settings
SecurityOptions advancedSecurity = new SecurityOptions();
advancedSecurity.setUserPassword("user123");
advancedSecurity.setOwnerPassword("owner456");

// Restrict permissions
advancedSecurity.setAllowPrint(false);
advancedSecurity.setAllowCopy(false);
advancedSecurity.setAllowEditContent(false);
advancedSecurity.setAllowEditAnnotations(false);
// Advanced security settings
SecurityOptions advancedSecurity = new SecurityOptions();
advancedSecurity.setUserPassword("user123");
advancedSecurity.setOwnerPassword("owner456");

// Restrict permissions
advancedSecurity.setAllowPrint(false);
advancedSecurity.setAllowCopy(false);
advancedSecurity.setAllowEditContent(false);
advancedSecurity.setAllowEditAnnotations(false);
JAVA

PDF의 SecurityManager를 통해 보안을 적용하세요:

// Apply security options to the PDF
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);

// Save the password-protected PDF document
urlToPdf.saveAs("protected.pdf");
// Apply security options to the PDF
SecurityManager securityManager = urlToPdf.getSecurity();
securityManager.setSecurityOptions(securityOptions);

// Save the password-protected PDF document
urlToPdf.saveAs("protected.pdf");
JAVA

PDF 파일을 열면 암호를 입력하라는 메시지가 나타납니다.

Password entry dialog for protected PDF with input field and Open file/Cancel buttons

올바른 비밀번호를 입력하면 PDF 파일이 정상적으로 열립니다.

IronPDF .NET homepage showing HTML to PDF code examples and download options

추가 설정은 보안 및 메타데이터 예제를 참조하세요.

전체 소스 코드는 무엇인가요?

이 튜토리얼의 전체 소스 코드는 다음과 같습니다.

// Import statement for IronPDF Java  
import com.ironsoftware.ironpdf.*;
import java.io.IOException;  
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) throws IOException {
        // Apply your license key
        License.setLicenseKey("Your License Key");

        // Convert HTML string to a PDF and save it
        String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
        pdf.saveAs(Paths.get("html.pdf"));

        // Convert HTML file to a PDF and save it
        PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
        myPdf.saveAs(Paths.get("html_file_saved.pdf"));

        // Convert URL to a PDF and save it
        PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
        urlToPdf.saveAs(Paths.get("urlToPdf.pdf"));

        // Password-protect the PDF file
        SecurityOptions securityOptions = new SecurityOptions();
        securityOptions.setUserPassword("shareable");
        SecurityManager securityManager = urlToPdf.getSecurity();
        securityManager.setSecurityOptions(securityOptions);
        urlToPdf.saveAs(Paths.get("protected.pdf"));
    }
}
// Import statement for IronPDF Java  
import com.ironsoftware.ironpdf.*;
import java.io.IOException;  
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) throws IOException {
        // Apply your license key
        License.setLicenseKey("Your License Key");

        // Convert HTML string to a PDF and save it
        String htmlString = "<h1>Hello World!</h1><p>This is an example HTML string.</p>";
        PdfDocument pdf = PdfDocument.renderHtmlAsPdf(htmlString);
        pdf.saveAs(Paths.get("html.pdf"));

        // Convert HTML file to a PDF and save it
        PdfDocument myPdf = PdfDocument.renderHtmlFileAsPdf("example.html");
        myPdf.saveAs(Paths.get("html_file_saved.pdf"));

        // Convert URL to a PDF and save it
        PdfDocument urlToPdf = PdfDocument.renderUrlAsPdf("https://ironpdf.com");
        urlToPdf.saveAs(Paths.get("urlToPdf.pdf"));

        // Password-protect the PDF file
        SecurityOptions securityOptions = new SecurityOptions();
        securityOptions.setUserPassword("shareable");
        SecurityManager securityManager = urlToPdf.getSecurity();
        securityManager.setSecurityOptions(securityOptions);
        urlToPdf.saveAs(Paths.get("protected.pdf"));
    }
}
JAVA

IronPDF는 서식 손실 없이 이미지와 텍스트를 렌더링합니다. 버튼은 계속 클릭 가능하고, 텍스트 상자는 계속 편집 가능합니다. 이 라이브러리는 서명 추가 , 차트 렌더링PDF 인쇄를 지원합니다.

핵심 요점은 무엇인가요?

이 가이드에서는 IronPDF를 사용하여 Java로 PDF를 생성하는 방법을 설명합니다. IronPDF는 HTML 파일, XML 문서 또는 기타 소스에서 PDF를 생성하기 위한 간단한 API를 제공합니다.

보고서, 송장 또는 기타 모든 유형의 문서를 신속하게 생성하세요. AWS , Azure 또는 Google Cloud 에 배포하세요.

IronPDF는 $799부터 시작하는 상업용 라이선스가 필요합니다. 제품 테스트를 위한 무료 체험판을 이용해 보세요 .

IronPDF 자바 라이브러리를 다운로드하세요 .

자주 묻는 질문

자바에서 PDF 파일을 만드는 가장 쉬운 방법은 무엇일까요?

Java에서 PDF를 생성하는 가장 쉬운 방법은 IronPDF의 renderHtmlAsPdf() 메서드를 사용하는 것입니다. 이 메서드에 HTML 문자열을 전달하고 결과를 저장하기만 하면 됩니다. 예: PdfDocument pdf = PdfDocument.renderHtmlAsPdf("Hello World!"); pdf.saveAs(Paths.get("output.pdf"));

IronPDF를 Maven 프로젝트에 추가하려면 어떻게 해야 하나요?

Maven 프로젝트에 IronPDF를 추가하려면 pom.xml 파일에 다음 종속성을 추가하세요. com.ironsoftware IronPDF 1.0.0

기존 HTML 파일을 PDF 형식으로 변환할 수 있나요?

네, IronPDF는 HTML 파일을 PDF로 변환하기 위한 renderHtmlFileAsPdf() 메서드를 제공합니다. 이 메서드는 파일 시스템에서 HTML 파일을 읽어 PDF 문서로 변환합니다.

자바를 사용하여 웹 페이지에서 PDF 파일을 생성하는 방법은 무엇인가요?

IronPDF는 웹 페이지를 PDF로 직접 변환하는 renderUrlAsPdf() 메서드를 제공합니다. 변환하려는 웹 페이지의 URL만 제공하면 IronPDF가 해당 페이지를 PDF 문서로 렌더링합니다.

프로그래밍 방식으로 어떤 유형의 비즈니스 문서를 생성할 수 있나요?

IronPDF는 송장, 보고서, 양식 및 기타 주문형 문서를 포함한 다양한 비즈니스 문서를 자동으로 생성할 수 있도록 지원합니다. 이 라이브러리는 사용자 지정 워터마크, PDF 압축 및 양식 생성과 같은 고급 기능을 지원합니다.

비밀번호로 보호된 PDF 파일을 만들 수 있을까요?

네, IronPDF는 PDF 파일에 암호 암호화를 지원합니다. 암호로 보호된 PDF 파일을 원하는 디렉터리로 내보낼 수 있어 중요한 비즈니스 정보의 보안을 강화할 수 있습니다.

Java 환경에서 IronPDF를 사용하기 위한 시스템 요구 사항은 무엇입니까?

Java에서 IronPDF를 사용하려면 애플리케이션 컴파일 및 실행을 위한 Java 개발 키트(JDK), 종속성 관리를 위한 Maven 또는 Gradle, 그리고 프로젝트에 IronPDF 라이브러리를 종속성으로 추가해야 합니다.

새로운 PDF 파일을 만드는 것뿐만 아니라 기존 PDF 파일을 수정할 수도 있나요?

네, IronPDF는 PDF 생성뿐만 아니라 다양한 PDF 조작 작업도 지원합니다. 라이브러리의 포괄적인 기능을 활용하여 여러 PDF 파일을 병합하거나 분할하고, 텍스트와 데이터를 추출하고, 파일 형식을 변환할 수 있습니다.

커티스 차우
기술 문서 작성자

커티스 차우는 칼턴 대학교에서 컴퓨터 과학 학사 학위를 취득했으며, Node.js, TypeScript, JavaScript, React를 전문으로 하는 프론트엔드 개발자입니다. 직관적이고 미적으로 뛰어난 사용자 인터페이스를 만드는 데 열정을 가진 그는 최신 프레임워크를 활용하고, 잘 구성되고 시각적으로 매력적인 매뉴얼을 제작하는 것을 즐깁니다.

커티스는 개발 분야 외에도 사물 인터넷(IoT)에 깊은 관심을 가지고 있으며, 하드웨어와 소프트웨어를 통합하는 혁신적인 방법을 연구합니다. 여가 시간에는 게임을 즐기거나 디스코드 봇을 만들면서 기술에 대한 애정과 창의성을 결합합니다.

시작할 준비 되셨나요?
버전: 2026.2 방금 출시되었습니다