HTML을 PDF로 변환: Python
이 가이드는 Python 개발자가 IronPDF 라이브러리를 사용하여 HTML 콘텐츠를 고품질 PDF(휴대용 문서 형식) 파일로 변환하는 방법에 대한 단계별 지침을 제공합니다.
IronPDF 는 .NET , Java , Python을 비롯한 다양한 프로그래밍 언어를 지원하는 종합적인 PDF 변환 및 처리 라이브러리입니다. 이 튜토리얼은 특히 Python 스크립트에서 IronPDF 사용하여 파일 또는 마크업 형태의 HTML 콘텐츠를 변환하는 데 중점을 둡니다.
.NET 애플리케이션에서 HTML을 PDF로 변환하는 방법 에 대한 별도의 튜토리얼도 제공됩니다.
개요
Python을 사용하여 HTML을 PDF로 변환하는 방법

- HTML을 PDF로 변환하는 데 필요한 Python 라이브러리를 설치하세요.
- `RenderHtmlAsPdf` 메서드를 사용하여 HTML 문자열을 PDF 문서로 변환합니다.
- Python을 사용하여 웹사이트 URL에서 PDF 파일을 직접 생성합니다.
- `RenderHtmlFileAsPdf` 메서드를 사용하여 HTML 파일을 PDF 파일로 변환합니다.
- 생성된 PDF 파일을 새 파일로 저장하세요.
시작하기
1. Python용 IronPDF PDF 라이브러리 설치
Python용 IronPDF 라이브러리를 설치하려면 널리 사용되는 패키지 관리자인 pip를 사용하면 됩니다. 다음 명령어를 실행하기만 하면 됩니다.
pip install ironpdf
==2023.x.x. 예를 들어, 다음 명령어를 실행할 수 있습니다:pip install ironpdf==2023.x.xpip install ironpdf==2023.x.x사용 방법 안내 및 코드 예제
2. HTML을 PDF로 변환
다음 섹션에서는 HTML을 PDF로 변환하는 IronPDF 의 뛰어난 렌더링 기능에 대해 자세히 살펴보겠습니다.
PDF 문서 렌더링의 기본 구성 요소는 ChromePdfRenderer 클래스입니다. 또한, PdfDocument 클래스는 다양한 조작 기능을 제공합니다. IronPDF는 HTML 콘텐츠를 PDF 문서로 변환할 수 있는 신뢰할 수 있는 방법을 제공하며, three key scenarios에 대응합니다:
- HTML 문자열/마크업을 PDF로 변환
- HTML 파일/압축 파일을 PDF로 변환
- URL을 PDF로 변환
이 섹션에서는 각 사용 사례에 대한 간략한 개요와 함께 자세한 내용을 확인할 수 있는 추가 자료를 제공합니다.
2.1 IronPDF 패키지 가져오기
IronPDF 가져오려면 IronPDF 사용할 소스 파일의 시작 부분에 다음 가져오기 문을 포함하십시오.
# Import statement for IronPDF for Python
from ironpdf import *# Import statement for IronPDF for Python
from ironpdf import *2.2 라이선스 키 설정 (선택 사항)
IronPDF for Python은 무료로 사용할 수 있지만, 무료 사용자의 PDF 파일에 타일 형태의 배경 워터마크를 추가합니다.
라이선스 페이지를 방문하여 라이선스 키를 받으시고 워터마크 없는 PDF를 이용하세요.
IronPDF 사용하여 워터마크 없는 PDF를 생성하려면 라이브러리에 유효한 라이선스 키를 제공해야 합니다. 다음 코드 조각은 라이선스 키를 사용하여 라이브러리를 구성하는 방법을 보여줍니다.
# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"# Apply your license key
License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"PDF 파일을 생성하거나 내용을 수정하기 전에 라이선스 키가 설정되어 있는지 확인하십시오. 다른 코드 라인 전에 LicenseKey 메서드를 호출하는 것이 권장됩니다. 라이선스 키는 당사 라이선스 페이지에서 구매하시거나 , 무료 평가판 라이선스 키를 받으시 려면 당사에 문의하십시오.
2.3 로그 파일 위치 설정 (선택 사항)
IronPDF는 Python 스크립트와 동일한 디렉토리에 Default.log라는 텍스트 파일에 로그 메시지를 생성할 수 있습니다.
로그 파일 이름과 위치를 사용자 정의하려면, 아래 코드 스니펫을 사용하여 LogFilePath 속성을 설정할 수 있습니다:
# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.All# Set a log path
Logger.EnableDebugging = True
Logger.LogFilePath = "Custom.log"
Logger.LoggingMode = Logger.LoggingModes.AllLogger.LogFilePath은 PDF 변환 및 조작 메서드를 사용하기 전에 호출되어야 합니다.
2.4 HTML 문자열에서 PDF 생성하기
RenderHtmlAsPdf 메서드는 HTML 문자열을 PDF 형식 문서로 변환합니다.
아래 코드 조각은 제목 요소 하나만 포함된 HTML 문자열에서 PDF 파일을 생성하는 방법을 보여줍니다.
from ironpdf import *
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF!</h1>")
# Export to a file or Stream
pdf.SaveAs("output.pdf")from ironpdf import *
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a HTML string using Python
pdf = renderer.RenderHtmlAsPdf("<h1>Hello from IronPDF!</h1>")
# Export to a file or Stream
pdf.SaveAs("output.pdf")RenderHtmlAsPdf 메서드는 최신 브라우저와 동일한 방식으로 HTML, CSS 및 JavaScript 처리하여 콘텐츠를 정확하게 렌더링합니다. 이 기능을 통해 소프트웨어 엔지니어는 웹 브라우저에서 보이는 것과 매우 유사한 PDF를 생성할 수 있습니다.
또한, RenderHtmlAsPdf 메서드는 로컬 또는 네트워크 폴더에 위치한 이미지, 스타일시트 및 스크립트와 같은 외부 리소스를 처리할 수 있습니다. 다음 예제는 assets 폴더에 저장된 CSS 파일 및 이미지를 참조하는 HTML에서 PDF 문서를 생성하는 방법을 보여줍니다:
from ironpdf import *
html = """
<html>
<head>
<title>Hello world!</title>
<link rel='stylesheet' href='assets/style.css'>
</head>
<body>
<h1>Hello from IronPDF!</h1>
<a href="https://ironpdf.com/python/"><img src='assets/logo.png' /></a>
</body>
</html>
"""
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")from ironpdf import *
html = """
<html>
<head>
<title>Hello world!</title>
<link rel='stylesheet' href='assets/style.css'>
</head>
<body>
<h1>Hello from IronPDF!</h1>
<a href="https://ironpdf.com/python/"><img src='assets/logo.png' /></a>
</body>
</html>
"""
renderer = ChromePdfRenderer()
pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")또한 개발자는 RenderHtmlAsPdf 메서드에 두 번째 인수를 제공하여 웹 자산을 참조할 기본 경로를 지정할 수 있습니다. 이 경로는 파일 시스템의 로컬 디렉터리 또는 URL 경로를 가리킬 수 있습니다.
RenderHtmlAsPdf 메서드를 사용하는 방법에 대한 더 나은 이해를 얻으려면 이 코드 예제를 참조하거나 API 참조 페이지를 참조하십시오.
2.5 URL에서 PDF 생성하기
IronPDF가 제공하는 RenderUrlAsPdf 메서드를 사용하여 웹사이트 URL을 PDF 문서로 변환할 수 있습니다.
다음은 위키백과 문서를 PDF 콘텐츠로 변환하는 방법을 보여주는 예시입니다.
from ironpdf import *
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF")
# Export to a file or Stream
pdf.SaveAs("url.pdf")from ironpdf import *
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF")
# Export to a file or Stream
pdf.SaveAs("url.pdf")더 자세한 내용은 웹페이지를 PDF로 변환하는 방법을 보여주는 코드 예제를 참조하십시오.
2.6 HTML 파일에서 PDF 생성하기
IronPDF HTML 파일을 PDF로 변환하여 로컬 파일 시스템에 저장하는 기능을 제공합니다. 이 기능은 HTML 콘텐츠를 해당 PDF 형식으로 직접 변환합니다.
For a real-world demonstration of this functionality, the following code example showcases the conversion of an invoice HTML file. You can access the HTML markup of the invoice.
이 HTML 마크업은 사용자의 편의를 위해 제공됩니다.
<html>
<head>
<meta charset="utf-8">
<title>Invoice</title>
<link rel="stylesheet" href="style.css">
<link rel="license" href="https://www.opensource.org/licenses/mit-license/">
<script src="script.js"></script>
</head>
<body>
<header>
<h1>Invoice</h1>
<address contenteditable>
<p>Jonathan Neal</p>
<p>101 E. Chapman Ave<br>Orange, CA 92866</p>
<p>(800) 555-1234</p>
</address>
<span><img alt="" src="http://www.jonathantneal.com/examples/invoice/logo.png"><input type="file" accept="image/*"></span>
</header>
<article>
<h1>Recipient</h1>
<address contenteditable>
<p>Some Company<br>c/o Some Guy</p>
</address>
<table class="meta">
<tr>
<th><span contenteditable>Invoice #</span></th>
<td><span contenteditable>101138</span></td>
</tr>
<tr>
<th><span contenteditable>Date</span></th>
<td><span contenteditable>January 1, 2012</span></td>
</tr>
<tr>
<th><span contenteditable>Amount Due</span></th>
<td><span id="prefix" contenteditable>$</span><span>600.00</span></td>
</tr>
</table>
<table class="inventory">
<thead>
<tr>
<th><span contenteditable>Item</span></th>
<th><span contenteditable>Description</span></th>
<th><span contenteditable>Rate</span></th>
<th><span contenteditable>Quantity</span></th>
<th><span contenteditable>Price</span></th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
<td><span contenteditable>Experience Review</span></td>
<td><span data-prefix>$</span><span contenteditable>150.00</span></td>
<td><span contenteditable>4</span></td>
<td><span data-prefix>$</span><span>600.00</span></td>
</tr>
</tbody>
</table>
<a class="add">+</a>
<table class="balance">
<tr>
<th><span contenteditable>Total</span></th>
<td><span data-prefix>$</span><span>600.00</span></td>
</tr>
<tr>
<th><span contenteditable>Amount Paid</span></th>
<td><span data-prefix>$</span><span contenteditable>0.00</span></td>
</tr>
<tr>
<th><span contenteditable>Balance Due</span></th>
<td><span data-prefix>$</span><span>600.00</span></td>
</tr>
</table>
</article>
<aside>
<h1><span contenteditable>Additional Notes</span></h1>
<div contenteditable>
<p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
</div>
</aside>
</body>
</html><html>
<head>
<meta charset="utf-8">
<title>Invoice</title>
<link rel="stylesheet" href="style.css">
<link rel="license" href="https://www.opensource.org/licenses/mit-license/">
<script src="script.js"></script>
</head>
<body>
<header>
<h1>Invoice</h1>
<address contenteditable>
<p>Jonathan Neal</p>
<p>101 E. Chapman Ave<br>Orange, CA 92866</p>
<p>(800) 555-1234</p>
</address>
<span><img alt="" src="http://www.jonathantneal.com/examples/invoice/logo.png"><input type="file" accept="image/*"></span>
</header>
<article>
<h1>Recipient</h1>
<address contenteditable>
<p>Some Company<br>c/o Some Guy</p>
</address>
<table class="meta">
<tr>
<th><span contenteditable>Invoice #</span></th>
<td><span contenteditable>101138</span></td>
</tr>
<tr>
<th><span contenteditable>Date</span></th>
<td><span contenteditable>January 1, 2012</span></td>
</tr>
<tr>
<th><span contenteditable>Amount Due</span></th>
<td><span id="prefix" contenteditable>$</span><span>600.00</span></td>
</tr>
</table>
<table class="inventory">
<thead>
<tr>
<th><span contenteditable>Item</span></th>
<th><span contenteditable>Description</span></th>
<th><span contenteditable>Rate</span></th>
<th><span contenteditable>Quantity</span></th>
<th><span contenteditable>Price</span></th>
</tr>
</thead>
<tbody>
<tr>
<td><a class="cut">-</a><span contenteditable>Front End Consultation</span></td>
<td><span contenteditable>Experience Review</span></td>
<td><span data-prefix>$</span><span contenteditable>150.00</span></td>
<td><span contenteditable>4</span></td>
<td><span data-prefix>$</span><span>600.00</span></td>
</tr>
</tbody>
</table>
<a class="add">+</a>
<table class="balance">
<tr>
<th><span contenteditable>Total</span></th>
<td><span data-prefix>$</span><span>600.00</span></td>
</tr>
<tr>
<th><span contenteditable>Amount Paid</span></th>
<td><span data-prefix>$</span><span contenteditable>0.00</span></td>
</tr>
<tr>
<th><span contenteditable>Balance Due</span></th>
<td><span data-prefix>$</span><span>600.00</span></td>
</tr>
</table>
</article>
<aside>
<h1><span contenteditable>Additional Notes</span></h1>
<div contenteditable>
<p>A finance charge of 1.5% will be made on unpaid balances after 30 days.</p>
</div>
</aside>
</body>
</html>"invoices"라는 폴더에 HTML 파일과 해당 CSS 및 JavaScript 파일이 저장되어 있다고 가정하면, 다음 Python 코드를 사용하여 IronPDF 로 샘플 HTML 파일을 PDF로 변환할 수 있습니다.
from ironpdf import *
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("invoices/TestInvoice1.html")
# Export to a file or Stream
pdf.SaveAs("htmlfile_to_pdf.pdf")from ironpdf import *
# Instantiate Renderer
renderer = ChromePdfRenderer()
# Create a PDF from an existing HTML file using Python
pdf = renderer.RenderHtmlFileAsPdf("invoices/TestInvoice1.html")
# Export to a file or Stream
pdf.SaveAs("htmlfile_to_pdf.pdf")HTML 문자열을 PDF로 변환하는 것과 유사하게, IronPDF 샘플 HTML 파일의 상대 URL을 자동으로 해석하여 참조된 스타일시트와 스크립트가 결과 PDF 문서에 올바르게 적용되도록 합니다. 이렇게 하면 웹 페이지의 시각적 모양이 PDF 파일에 정확하게 담기게 됩니다.
3. 추가 자료
IronPDF의 HTML-PDF 변환 기능이 제공하는 다양한 활용법을 알아보려면 코드 예제 섹션을 참조하십시오.
- 이 코드 예제를 읽고 PDF 문서 변환 과정에서 문서의 모양을 사용자 지정하는 방법을 알아보세요.
- 개인화된 머리글과 바닥글이 있는 PDF 파일을 생성하는 방법, 여백 크기 와 페이지 크기를 조정하는 방법, 워터마크를 추가하는 방법 등을 알아보세요.
- 또한 텍스트 추출 , 파일 크기 최적화 및 PDF를 프로그램적으로 인쇄하는 기술을 살펴보십시오.
소프트웨어 제품을 다운로드하세요 .
자주 묻는 질문
Python을 사용하여 HTML을 PDF로 변환하는 방법은 무엇인가요?
IronPDF의 RenderHtmlAsPdf 메서드를 사용하면 Python에서 HTML 문자열을 PDF 문서로 변환할 수 있습니다. 이 메서드는 CSS 및 JavaScript를 포함한 HTML을 고품질 PDF로 정확하게 렌더링합니다.
Python에서 HTML을 PDF로 변환하는 기능을 지원하는 라이브러리는 무엇인가요?
IronPDF는 Python에서 HTML을 PDF로 변환하는 강력한 라이브러리입니다. HTML 문자열, URL 및 파일을 서식을 유지하면서 PDF 문서로 변환하는 다양한 메서드를 제공합니다.
Python에서 HTML을 PDF로 변환하는 IronPDF를 어떻게 설치하나요?
Python용 IronPDF를 설치하려면 터미널에서 pip install ironpdf 명령어를 사용하세요. IronPDF는 .NET 6.0 SDK에 의존하므로 .NET 6.0 SDK가 설치되어 있어야 합니다.
Python으로 웹페이지 URL을 PDF로 변환할 수 있나요?
네, IronPDF의 RenderUrlAsPdf 메서드를 사용하여 웹페이지 URL을 PDF 문서로 변환할 수 있습니다. 이 기능을 사용하면 실시간 웹페이지의 콘텐츠를 직접 캡처하고 변환할 수 있습니다.
IronPDF는 변환 과정에서 외부 리소스를 어떻게 처리하나요?
IronPDF는 이미지, 스타일시트, 스크립트와 같은 외부 리소스를 포함하는 HTML 파일을 처리할 수 있습니다. 상대 URL을 해석하여 이러한 리소스가 PDF에 올바르게 포함되도록 합니다.
Python을 사용하여 PDF 머리글과 바닥글을 사용자 정의할 수 있을까요?
네, IronPDF를 사용하면 PDF 문서에 사용자 지정 머리글과 바닥글을 추가할 수 있습니다. PdfDocument 속성을 조정하여 특정 서식 요구 사항에 맞게 설정할 수 있습니다.
IronPDF로 만든 PDF에서 워터마크를 제거하려면 어떻게 해야 하나요?
IronPDF로 생성된 PDF에서 워터마크를 제거하려면 PDF를 처리하기 전에 유효한 라이선스 키를 설정해야 합니다. 이렇게 하면 기본 워터마크가 없는 PDF가 생성됩니다.
IronPDF의 HTML을 PDF로 변환하는 고급 기능에는 어떤 것들이 있나요?
IronPDF는 사용자 지정 페이지 여백 설정, 크기 조정, 워터마크 추가, 파일 크기 최적화, 텍스트 추출 등과 같은 고급 기능을 제공합니다. 이러한 기능은 라이브러리의 유연성과 활용도를 높여줍니다.
IronPDF를 사용하여 HTML을 PDF로 변환하는 예제는 어디에서 찾을 수 있나요?
IronPDF 웹사이트의 '코드 예제' 섹션에서 다양한 예제와 자세한 코드 조각을 찾아볼 수 있으며, 이를 통해 IronPDF의 기능을 효과적으로 사용하는 방법을 알 수 있습니다.
IronPDF에서 PDF를 렌더링하는 데 사용되는 주요 클래스는 무엇입니까?
ChromePdfRenderer 클래스는 IronPDF에서 PDF를 렌더링하는 데 사용되는 주요 클래스입니다. 이 클래스는 HTML 변환, 외부 리소스 처리, 최신 브라우저와 유사한 정확한 렌더링을 보장하는 안정적인 메서드를 제공합니다.
IronPDF는 .NET 10과 호환됩니까?
네, IronPDF는 .NET 6, 7, 8, 9와 같은 이전 버전과 마찬가지로 .NET 10과 완벽하게 호환됩니다. 플랫폼(Windows, Linux 등)에 관계없이 .NET 10 프로젝트에서 별도의 설정 없이 바로 사용할 수 있으며, 최신 API 스택을 특별한 해결 방법 없이 지원합니다.










