C# .NET Core P/Invoke wrapper for wkhtmltopdf library that uses WebKit to convert HTML to PDF and Image.
![]() | HTMLconvert is a lightweight and easy to use wrapper for wkhtmltopdf library that will help you convert HTML to PDF/Image bytes. |
You can install the library via Nuget (HTMLconvert.NETCore). Run below command from package manager console:
PM> Install-Package HTMLconvert.NETCore -Version 1.0.0
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.debsudo apt install ./wkhtmltox_0.12.6-1.focal_amd64.debwget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.bionic_amd64.debsudo apt install ./wkhtmltox_0.12.6-1.bionic_amd64.debwget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.xenial_amd64.debsudo apt install ./wkhtmltox_0.12.6-1.xenial_amd64.debwget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.buster_amd64.debsudo apt install ./wkhtmltox_0.12.6-1.buster_amd64.debwget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.stretch_amd64.debsudo apt install ./wkhtmltox_0.12.6-1.stretch_amd64.deb$ brew update && brew cask install wkhtmltopdfvarimageConverter=newConverter(newImageTools(useGraphics:false));varresult=imageConverter.Convert(newImageSettings(){Format="png",SmartWidth=true,Quality=100,In="https://google.com/"});varpathToImage=Path.Combine(Directory.GetCurrentDirectory(),"web.png");File.Delete(pathToImage);File.WriteAllBytesAsync(pathToImage,result);// Write bytes to .png filevarhtmlContent=@"<!DOCTYPE html><html><head><title>HTML Tables</title></head><body><table><tr><th>First_Name</th><th>Last_Name</th><th>Marks</th></tr><tr><td>Sonoo</td><td>Jaiswal</td><td>60</td></tr><tr><td>James</td><td>William</td><td>80</td></tr><tr><td>Swati</td><td>Sironi</td><td>82</td></tr><tr><td>Chetna</td><td>Singh</td><td>72</td></tr></table></body></html>";varimageConverter=newConverter(newImageTools(useGraphics:false));varresult=imageConverter.Convert(newImageSettings(){Format="png",SmartWidth=true,Quality=100,},htmlContent);varpathToImage=Path.Combine(Directory.GetCurrentDirectory(),"HTMLcontent.png");File.Delete(pathToImage);File.WriteAllBytesAsync(pathToImage,result);// Write bytes to .png filevarpdfConverter=newConverter(newPdfTools(useGraphics:false));varresult=pdfConverter.Convert(newPdfSettings(){ColorMode=ColorMode.Color,Orientation=Orientation.Portrait,PaperSize=PaperKind.A4,Margins=newMarginSettings(){Top=10}},newObjectSettings(){Page="http://google.com/"});varpathToPdf=Path.Combine(Directory.GetCurrentDirectory(),"DocumentWeb.pdf");File.Delete(pathToPdf);File.WriteAllBytesAsync(pathToPdf,result);// Write bytes to .pdf filevarhtmlContent=@"<!DOCTYPE html><html><head><title>HTML Tables</title></head><body><table><tr><th>First_Name</th><th>Last_Name</th><th>Marks</th></tr><tr><td>Sonoo</td><td>Jaiswal</td><td>60</td></tr><tr><td>James</td><td>William</td><td>80</td></tr><tr><td>Swati</td><td>Sironi</td><td>82</td></tr><tr><td>Chetna</td><td>Singh</td><td>72</td></tr></table></body></html>";varpdfConverter=newConverter(newPdfTools(useGraphics:false));varresult=pdfConverter.Convert(newPdfSettings(){ColorMode=ColorMode.Color,Orientation=Orientation.Landscape,PaperSize=PaperKind.A4Plus,},htmlContent,newObjectSettings(){PagesCount=true,WebSettings={DefaultEncoding="utf-8"},HeaderSettings={FontSize=9,Right="Page [page] of [toPage]",Line=true,Spacing=2.812}});varpathToPdf=Path.Combine(Directory.GetCurrentDirectory(),"DocumentHTML.pdf");File.Delete(pathToPdf);File.WriteAllBytesAsync(pathToPdf,result);// Write bytes to .pdf file