.NET에서 HTML을 PDF로 변환

개요

이 문서에서는 C#을 사용하여 HTML을 PDF로 변환하는 방법을 설명합니다. 다음 주제를 다룹니다.

다음 코드 스니펫은 Aspose.PDF.Drawing 라이브러리와도 작동합니다.

형식: HTML

형식: MHTML

형식: 웹 페이지

C# HTML을 PDF로 변환

Aspose.PDF for .NET은 기존 HTML 문서를 PDF로 원활하게 변환할 수 있는 PDF 조작 API입니다. HTML을 PDF로 변환하는 과정은 유연하게 사용자 정의할 수 있습니다.

HTML을 PDF로 변환

다음 C# 코드 샘플은 HTML 문서를 PDF로 변환하는 방법을 보여줍니다.

단계: C#에서 HTML을 PDF로 변환하기

  1. HtmlLoadOptions 클래스의 인스턴스를 생성합니다.
  2. Document 객체를 초기화합니다.
  3. Document.Save() 메서드를 호출하여 출력 PDF 문서를 저장합니다.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Load the HTML file into a document using HtmlLoadOptions
    var options = new Aspose.Pdf.HtmlLoadOptions();

    // Open HTML document
    using (var document = new Aspose.Pdf.Document(dataDir + "test.html", options))
    {
        // Save PDF document
        document.Save(dataDir + "ConvertHTMLtoPDF_out.pdf");
    }
}

HTML에서 PDF로의 고급 변환

HTML 변환 엔진에는 변환 프로세스를 제어할 수 있는 여러 옵션이 있습니다.

미디어 쿼리 지원

미디어 쿼리는 다양한 장치에 맞춤형 스타일 시트를 제공하는 인기 있는 기술입니다. HtmlMediaType 속성을 사용하여 장치 유형을 설정할 수 있습니다.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDFAdvancedMediaType()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Load the HTML file into a document using HtmlLoadOptions with Print media type
    var options = new HtmlLoadOptions
    {
        // Set Print or Screen mode
        HtmlMediaType = Aspose.Pdf.HtmlMediaType.Print
    };

    // Open HTML document
    using (var document = new Aspose.Pdf.Document(dataDir + "test.html", options))
    {
        // Save PDF document
        document.Save(dataDir + "ConvertHTMLtoPDFAdvancedMediaType_out.pdf");
    }
}

글꼴 포함 활성화(비활성화)

HTML 페이지는 종종 글꼴(예: 로컬 폴더의 글꼴, Google Fonts 등)을 사용합니다. IsEmbedFonts 속성을 사용하여 문서에서 글꼴 포함을 제어할 수 있습니다.

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ConvertHTMLtoPDFAdvancedEmbedFonts()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Load the HTML file into a document using HtmlLoadOptions with the font embedding option set
     var options = new Aspose.Pdf.HtmlLoadOptions
     {
         // Disable font embedding
         IsEmbedFonts = false
     };

     // Open HTML document
     using (var document = new Aspose.Pdf.Document(dataDir + "test_fonts.html", options))
     {
         // Save PDF document
         document.Save(dataDir + "ConvertHTMLtoPDFAdvanced_EmbedFonts_out.pdf");
     }
 }

외부 리소스 로딩 관리

변환 엔진은 HTML 문서와 관련된 특정 리소스의 로딩을 제어할 수 있는 메커니즘을 제공합니다. HtmlLoadOptions 클래스에는 리소스 로더의 동작을 정의할 수 있는 CustomLoaderOfExternalResources 속성이 있습니다. 모든 PNG 이미지를 단일 이미지 test.jpg로 교체하고 다른 리소스의 외부 URL을 내부로 교체해야 한다고 가정해 보겠습니다. 이를 위해 사용자 정의 로더 SamePictureLoader를 정의하고 CustomLoaderOfExternalResources를 이 이름으로 지정할 수 있습니다.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDFAdvanced_DummyImage()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Load the HTML file into a document with a custom resource loader for external images
    var options = new Aspose.Pdf.HtmlLoadOptions
    {
        CustomLoaderOfExternalResources = SamePictureLoader
    };

    // Open HTML document
    using (var document = new Aspose.Pdf.Document(dataDir + "test.html", options))
    {
        // Save PDF document
        document.Save(dataDir + "html_test.pdf");
    }
}

private static Aspose.Pdf.LoadOptions.ResourceLoadingResult SamePictureLoader(string resourceURI)
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();
    Aspose.Pdf.LoadOptions.ResourceLoadingResult result;

    if (resourceURI.EndsWith(".png"))
    {
        byte[] resultBytes = File.ReadAllBytes(dataDir + "test.jpg");
        result = new Aspose.Pdf.LoadOptions.ResourceLoadingResult(resultBytes)
        {
            // Set MIME Type
            MIMETypeIfKnown = "image/jpeg"
        };
    }
    else
    {
        result = new Aspose.Pdf.LoadOptions.ResourceLoadingResult(GetContentFromUrl(resourceURI));
    }
    return result;
}

private static byte[] GetContentFromUrl(string url)
{
    var httpClient = new System.Net.Http.HttpClient();
    return httpClient.GetByteArrayAsync(url).GetAwaiter().GetResult();
}

웹 페이지를 PDF로 변환

웹 페이지를 변환하는 것은 로컬 HTML 문서를 변환하는 것과 약간 다릅니다. 웹 페이지 내용을 PDF 형식으로 변환하기 위해 먼저 HttpClient 인스턴스를 사용하여 HTML 페이지 내용을 가져오고, Stream 객체를 생성하고, 내용을 Document 객체에 전달한 다음 PDF 형식으로 출력을 렌더링할 수 있습니다.

웹 서버에 호스팅된 웹 페이지를 PDF로 변환할 때:

단계: C#에서 웹 페이지를 PDF로 변환하기

  1. HttpClient 객체를 사용하여 페이지의 내용을 읽습니다.
  2. HtmlLoadOptions 객체를 인스턴스화하고 기본 URL을 설정합니다.
  3. Stream 객체를 전달하면서 Document 객체를 초기화합니다.
  4. 선택적으로 페이지 크기 및/또는 방향을 설정합니다.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDFAdvanced_WebPage()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    const string url = "https://en.wikipedia.org/wiki/Aspose_API";

    // Set page size A3 and Landscape orientation;   
    var options = new Aspose.Pdf.HtmlLoadOptions(url)
    {
        PageInfo =
        {
            Width = 842,
            Height = 1191,
            IsLandscape = true
        }
    };

    // Load the web page content as a stream and create a PDF document
    using (var document = new Aspose.Pdf.Document(GetContentFromUrlAsStream(url), options))
    {
        // Save PDF document
        document.Save(dataDir + "html_test.pdf");
    }
}

private static Stream GetContentFromUrlAsStream(string url, System.Net.ICredentials credentials = null)
{
    using (var handler = new System.Net.Http.HttpClientHandler { Credentials = credentials })
    using (var httpClient = new System.Net.Http.HttpClient(handler))
    {
        return httpClient.GetStreamAsync(url).GetAwaiter().GetResult();
    }
}

웹 페이지를 PDF로 변환하기 위한 자격 증명 제공

때때로 인증 및 접근 권한이 필요한 HTML 파일의 변환을 수행해야 하므로 인증된 사용자만 페이지 내용을 가져올 수 있습니다. 여기에는 HTML 내부에서 참조된 일부 리소스/데이터가 인증이 필요한 외부 서버에서 가져오는 시나리오도 포함됩니다. 이러한 요구 사항을 충족하기 위해 HtmlLoadOptions 클래스에 ExternalResourcesCredentials 속성이 추가되었습니다. 다음 코드 스니펫은 HTML 파일을 PDF로 변환할 때 HTML 및 해당 리소스에 대한 자격 증명을 요청하는 단계를 보여줍니다.

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ConvertHTMLtoPDFAdvancedAuthorized()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     const string url = "http://httpbin.org/basic-auth/user1/password1";
     var credentials = new System.Net.NetworkCredential("user1", "password1");

     var options = new Aspose.Pdf.HtmlLoadOptions(url)
     {
         ExternalResourcesCredentials = credentials
     };

     using (var document = new Aspose.Pdf.Document(GetContentFromUrlAsStream(url, credentials), options))
     {
         // Save PDF document
         document.Save(dataDir + "HtmlTest_out.pdf");
     }
 }

private static Stream GetContentFromUrlAsStream(string url, System.Net.ICredentials credentials = null)
{
    using (var handler = new System.Net.Http.HttpClientHandler { Credentials = credentials })
    using (var httpClient = new System.Net.Http.HttpClient(handler))
    {
        return httpClient.GetStreamAsync(url).GetAwaiter().GetResult();
    }
}

모든 HTML 내용을 단일 페이지에 렌더링

Aspose.PDF for .NET은 HTML 파일을 PDF 형식으로 변환할 때 모든 내용을 단일 페이지에 렌더링할 수 있는 기능을 제공합니다. 예를 들어 출력 크기가 한 페이지를 초과하는 HTML 내용이 있는 경우 출력 데이터를 단일 PDF 페이지로 렌더링하는 옵션을 사용할 수 있습니다. 이 옵션을 사용하기 위해 HtmlLoadOptions 클래스는 IsRenderToSinglePage 플래그로 확장되었습니다. 아래 코드 스니펫은 이 기능을 사용하는 방법을 보여줍니다.

 // For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
 private static void ConvertHTMLtoPDFAdvancedSinglePageRendering()
 {
     // The path to the documents directory
     var dataDir = RunExamples.GetDataDir_AsposePdf();

     // Initialize HtmlLoadOptions
     var options = new Aspose.Pdf.HtmlLoadOptions
     {
         // Set Render to single page property
         IsRenderToSinglePage = true
     };

     // Open PDF document
     using (var document = new Aspose.Pdf.Document(dataDir + "HTMLToPDF.html", options))
     {
         // Save PDF document
         document.Save(dataDir + "RenderContentToSamePage_out.pdf");
     }
 }

SVG 데이터로 HTML 렌더링

Aspose.PDF for .NET은 HTML 페이지를 PDF 문서로 변환하는 기능을 제공합니다. HTML은 페이지에 태그로 SVG 그래픽 요소를 추가할 수 있으므로 Aspose.PDF는 이러한 데이터를 결과 PDF 파일로 변환하는 것도 지원합니다. 다음 코드 스니펫은 SVG 그래픽 태그가 포함된 HTML 파일을 태그가 있는 PDF 문서로 변환하는 방법을 보여줍니다.

// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertHTMLtoPDFWithSVG()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Initialize HtmlLoadOptions
    var options = new Aspose.Pdf.HtmlLoadOptions(Path.GetDirectoryName(dataDir + "HTMLSVG.html"));

    // Initialize Document object
    using (var document = new Aspose.Pdf.Document(dataDir + "HTMLSVG.html", options))
    {
        // Save PDF document
        document.Save(dataDir + "RenderHTMLwithSVGData_out.pdf");
    }
}

MHTML을 PDF로 변환

MHTML, MIME HTML의 약자는 일반적으로 외부 링크(예: 이미지, Flash 애니메이션, Java 애플릿 및 오디오 파일)로 표시되는 리소스를 HTML 코드와 결합하여 단일 파일로 만드는 웹 페이지 아카이브 형식입니다. MHTML 파일의 내용은 MIME 유형 multipart/related를 사용하여 HTML 이메일 메시지인 것처럼 인코딩됩니다. Aspose.PDF for .NET은 HTML 파일을 PDF 형식으로 변환할 수 있으며, Aspose.PDF for .NET 9.0.0의 출시와 함께 MHTML 파일을 PDF 형식으로 변환할 수 있는 새로운 기능을 도입했습니다. 다음 코드 스니펫은 C#을 사용하여 MHTML 파일을 PDF 형식으로 변환하는 방법을 보여줍니다.

단계: C#에서 MHTML을 PDF로 변환하기

  1. MhtLoadOptions 클래스의 인스턴스를 생성합니다.
  2. Document 객체를 초기화합니다.
  3. Document.Save() 메서드를 호출하여 출력 PDF 문서를 저장합니다.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ConvertMHTtoPDF()
{
    // The path to the documents directory
    var dataDir = RunExamples.GetDataDir_AsposePdf();

    // Initialize MhtLoadOptions with page setup
    var options = new Aspose.Pdf.MhtLoadOptions()
    {
        PageInfo = { Width = 842, Height = 1191, IsLandscape = true }
    };

    // Initialize Document object using the MHT file and options
    using (var document = new Aspose.Pdf.Document(dataDir + "fileformatinfo.mht", options))
    {
        // Save PDF document
        document.Save(dataDir + "MhtmlTest_out.pdf");
    }
}

참조

이 문서에서는 다음 주제도 다룹니다. 코드가 위와 동일합니다.

형식: HTML

형식: MHTML

형식: 웹 페이지