Nuget 패키지 관리 -> 찾아보기 -> NReco.PdfGenerator -> 설치
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | using System.IO; using System.Text; namespace HtmlToPdf { class Program { static void Main( string [] args) { StringBuilder saveHtml = new StringBuilder(); saveHtml.Append( "<!DOCTYPE html>" ); saveHtml.Append( "<html lang=\"ko\">" ); saveHtml.Append( "<head>" ); saveHtml.Append( "<meta charset=\"utf-8\" />" ); saveHtml.Append( "<title>Html을 PDF로</title>" ); saveHtml.Append( "</head>" ); saveHtml.Append( "<body>" ); saveHtml.Append( "<h1>Html을 PDF로</h1><p>좋은하루입니다.</p>" ); saveHtml.Append( "</body>" ); saveHtml.Append( "</html>" ); // NReco 호출 var converter = new NReco.PdfGenerator.HtmlToPdfConverter(); // 저장할 파일명 string pdfFile = @"d:\test.pdf" ; // 파일이 있을경우 삭제 if (File.Exists(pdfFile)) { File.Delete(pdfFile); } // saveHtml을 pdf byte 형식으로 반환 byte [] f = converter.GeneratePdf(saveHtml.ToString()); // 파일을 저장 File.WriteAllBytes(pdfFile, f); } } } |
VB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | Imports System.IO Imports System.Text Module Module1 Sub Main() Dim saveHtml As StringBuilder = New StringBuilder saveHtml.Append( "<!DOCTYPE html>" ) saveHtml.Append( "<html lang=" "ko" ">" ) saveHtml.Append( "<head>" ) saveHtml.Append( "<meta charset=" "utf-8" " />" ) saveHtml.Append( "<title>Html을 PDF로</title>" ) saveHtml.Append( "</head>" ) saveHtml.Append( "<body>" ) saveHtml.Append( "<h1>Html을 PDF로</h1><p>좋은하루입니다.</p>" ) saveHtml.Append( "</body>" ) saveHtml.Append( "</html>" ) ' NReco 호출 Dim Converter = New NReco.PdfGenerator.HtmlToPdfConverter ' 저장할 파일명 Dim pdfFile As String = "d:\test.pdf" ' 파일이 있을경우 삭제 If File.Exists(pdfFile) Then File.Delete(pdfFile) End If ' saveHtml을 pdf byte 형식으로 반환 Dim f As Byte () = Converter.GeneratePdf(saveHtml.ToString()) ' 파일을 저장 File.WriteAllBytes(pdfFile, f) End Sub End Module |