Làm cách nào để chuyển đổi HTML sang PDF bằng Pdfsharp?

Một thẻ đã tồn tại với tên chi nhánh được cung cấp. Nhiều lệnh Git chấp nhận cả tên thẻ và tên nhánh, vì vậy việc tạo nhánh này có thể gây ra hành vi không mong muốn. Bạn có chắc chắn muốn tạo nhánh này không?

Gần đây, một vấn đề nhỏ thú vị mà chúng tôi phải thực hiện cho một khách hàng; . chương trình dòng lệnh net

Có rất nhiều thư viện của bên thứ 3 làm điều đó, nhưng thông thường, chúng đắt tiền và không đáng tin cậy lắm, định dạng HTML mà chúng tôi muốn định dạng khá kém. Vì vậy, chúng tôi đã đưa ra điều này

---------------
Public Function ConvertHTML[oldfilename As String, newfilename As String] As String

Dim pngfilename As String = Path.GetTempFileName[]

Dim res As String = "" ' = ok
____1
End Function

—————

Để điều này hoạt động, bạn sẽ cần thêm các thư viện sau vào dự án của mình

  • PDFSharp [miễn phí sử dụng; tải xuống từ http. //pdfsharp. mật mã. com/phát hành]
  • Hệ thống. các cửa sổ. Biểu mẫu [ngay cả khi bạn đang viết ứng dụng bảng điều khiển như chúng tôi]
  • Hệ thống. Đang vẽ

Có bắt được không? . Nó sử dụng chức năng không có giấy tờ trong đối tượng WebBrowser [DrawToBitmap], nhưng chức năng đó đã hoạt động bình thường đối với một số bản phát hành của Microsoft về. NET [ít nhất là từ năm 2010] và nếu trang web quá lớn, nó sẽ bị cắt bớt ở 3000px [một hạn chế mà tôi đã tích hợp vào đoạn mã trên để kiểm tra lỗi; bạn có thể xóa nó nếu muốn, mặc dù có giới hạn về kích thước

Mã của chúng tôi ghi vào một trang PDF, hoàn toàn vì đó là những gì chúng tôi cần cho khách hàng của mình – với một chút thao tác hình ảnh, thật đơn giản để trải mã đó trên nhiều trang PDF nếu cần

Gói

Try
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While

wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution

wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]

Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]

Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using

End Using

Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If

End Try

Return res
0 được sử dụng để tạo tệp pdf từ đoạn mã HTML trong C#. Gói này giúp dễ dàng chuyển đổi một chuỗi chứa đoạn mã HTML thành tài liệu PDF chứa chế độ xem web của đoạn mã HTML đó. Gói
Try
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While

wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution

wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]

Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]

Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using

End Using

Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If

End Try

Return res
0 là gói bên ngoài và không được cài đặt sẵn thư viện
Try
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While

wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution

wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]

Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]

Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using

End Using

Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If

End Try

Return res
2. Chúng tôi phải cài đặt riêng gói này bằng trình quản lý gói NuGet. Lệnh cài đặt gói
Try
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While

wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution

wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]

Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]

Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using

End Using

Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If

End Try

Return res
0 được đưa ra bên dưới

dotnet add package HtmlRenderer.PdfSharp --version 1.5.0.6

Ví dụ mã sau đây cho chúng ta thấy cách chuyển đổi một chuỗi chứa đoạn mã HTML thành tệp PDF bằng gói

Try
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While

wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution

wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]

Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]

Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using

End Using

Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If

End Try

Return res
0 trong C#

using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;

namespace html_to_pdf
{
    class Program
    {
        static void Main[string[] args]
        {
            string htmlString = "Document 

This is an HTML document which is converted to a pdf file.

"; PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4]; pdfDocument.Save["C:/File/HTML to PDF Document.pdf"]; } } }

Tệp

Try
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While

wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution

wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]

Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]

Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using

End Using

Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If

End Try

Return res
5

Trong đoạn mã trên, chúng tôi đã chuyển đổi đoạn mã HTML bên trong biến chuỗi

using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;

namespace html_to_pdf
{
    class Program
    {
        static void Main[string[] args]
        {
            string htmlString = "Document 

This is an HTML document which is converted to a pdf file.

"; PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4]; pdfDocument.Save["C:/File/HTML to PDF Document.pdf"]; } } }
0 thành tệp PDF với gói
Try
Using wb As System.Windows.Forms.WebBrowser = New System.Windows.Forms.WebBrowser
wb.ScrollBarsEnabled = False
wb.ScriptErrorsSuppressed = True
wb.Navigate[oldfilename]
While Not [wb.ReadyState = WebBrowserReadyState.Complete]
Application.DoEvents[]
End While

wb.Width = wb.Document.Body.ScrollRectangle.Width
wb.Height = wb.Document.Body.ScrollRectangle.Height
If wb.Height > 3000 Then
wb.Height = 3000
End If
' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
Dim b As Bitmap = New System.Drawing.Bitmap[wb.Width, wb.Height]
Dim hr As Integer = b.HorizontalResolution
Dim vr As Integer = b.VerticalResolution

wb.DrawToBitmap[b, New Rectangle[0, 0, wb.Width, wb.Height]]
wb.Dispose[]
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If
b.Save[pngfilename, Imaging.ImageFormat.Png]
b.Dispose[]

Using doc As PdfSharp.Pdf.PdfDocument = New PdfSharp.Pdf.PdfDocument
Dim page As PdfSharp.Pdf.PdfPage = New PdfSharp.Pdf.PdfPage[]
page.Width = PdfSharp.Drawing.XUnit.FromInch[wb.Width / hr]
page.Height = PdfSharp.Drawing.XUnit.FromInch[wb.Height / vr]
doc.Pages.Add[page]

Dim xgr As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage[page]
Dim img As PdfSharp.Drawing.XImage = PdfSharp.Drawing.XImage.FromFile[pngfilename]
xgr.DrawImage[img, 0, 0]
doc.Save[newfilename]
doc.Close[]
img.Dispose[]
xgr.Dispose[]
End Using

End Using

Catch ex As Exception
res = "Error: " & ex.Message
Finally
If File.Exists[pngfilename] Then
File.Delete[pngfilename]
End If

End Try

Return res
0 trong C#. Trước tiên, chúng tôi đã khởi tạo chuỗi
using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;

namespace html_to_pdf
{
    class Program
    {
        static void Main[string[] args]
        {
            string htmlString = "Document 

This is an HTML document which is converted to a pdf file.

"; PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4]; pdfDocument.Save["C:/File/HTML to PDF Document.pdf"]; } } }
0 bằng một đoạn mã HTML. Sau đó, chúng tôi đã tạo một thể hiện của lớp
using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;

namespace html_to_pdf
{
    class Program
    {
        static void Main[string[] args]
        {
            string htmlString = "Document 

This is an HTML document which is converted to a pdf file.

"; PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4]; pdfDocument.Save["C:/File/HTML to PDF Document.pdf"]; } } }
3 với hàm
using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;

namespace html_to_pdf
{
    class Program
    {
        static void Main[string[] args]
        {
            string htmlString = "Document 

This is an HTML document which is converted to a pdf file.

"; PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4]; pdfDocument.Save["C:/File/HTML to PDF Document.pdf"]; } } }
4 trong C#. Cuối cùng, chúng tôi đã lưu tài liệu vào một đường dẫn được chỉ định bằng hàm
using PdfSharp;
using PdfSharp.Pdf;
using System;
using System.IO;
using TheArtOfDev.HtmlRenderer.PdfSharp;

namespace html_to_pdf
{
    class Program
    {
        static void Main[string[] args]
        {
            string htmlString = "Document 

This is an HTML document which is converted to a pdf file.

"; PdfDocument pdfDocument = PdfGenerator.GeneratePdf[htmlString, PageSize.A4]; pdfDocument.Save["C:/File/HTML to PDF Document.pdf"]; } } }
5. Nội dung tệp có thể được nhìn thấy trong đoạn mã màn hình được hiển thị ở trên

Làm cách nào để chuyển đổi HTML sang PDF trong C#?

Chuyển đổi HTML sang PDF trong C# – Hướng dẫn đầy đủ .
Cài đặt và định cấu hình thư viện chuyển đổi HTML sang PDF trong dự án của bạn
Chuyển đổi tệp HTML sang PDF
Chuyển đổi URL hiện có sang PDF
Chuyển đổi ASP. NET Core Web Razor sang PDF
Chuyển đổi ASP. NET Core MVC xem sang PDF
Chuyển đổi biểu mẫu HTML sang Biểu mẫu PDF có thể điền

Làm cách nào để chuyển đổi HTML sang PDF bằng Itextsharp trong C#?

Phân tích chuỗi HTML bằng HTMLWorker của thư viện Itextsharp, HTMLWorker htmlparser = new HTMLWorker[pdfDoc]; .
Nhà văn PdfWriter = PdfWriter. GetInstance[pdfDoc, memoryStream];
pdfDoc. Mở ra[];
trình phân tích cú pháp html. Phân tích cú pháp [sr];
pdfDoc. Đóng[];

Làm cách nào để tạo PDF bằng PDFsharp trong C#?

Tạo tệp PDF trong C#. .
Bước 1 – Tạo Project kiểu Windows Forms App C#. NET. .
Bước 2 – Thêm tham chiếu vào PDF Sharp Library. .
Bước 3 – Thêm mã để tạo PDF bằng PDF Sharp. .
Bước 4 – Chạy và kiểm tra mã. .
Bước 5 – Xem tệp PDF đã tạo

Làm cách nào để chuyển đổi HTML sang PDF bằng Syncfusion?

Các bước để chuyển đổi HTML sang PDF trong. NET .
Bước 1. Tạo một cái mới. ứng dụng bảng điều khiển NET
Bước 2. Cài đặt đồng bộ hóa. HtmlToPdfChuyển đổi. Net. Gói Windows NuGet làm tài liệu tham khảo cho bạn. NET từ NuGet. tổ chức
Thực hiện chương trình bạn sẽ được tài liệu PDF như sau

Chủ Đề