The following example shows the most common use case for converting HTML document to PDF. The source HTML file is loaded from a current folder. The converted file is saved to the same folder.
usingSystem;usingGroupDocs.Conversion.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply the licenseLicense.Set(publicKey,privateKey);// Create a converter for the HTML filevarconverter=newHtmlToPdfConverter("sample.html");// Convert HTML to PDFconverter.Convert("sample.pdf");
ImportsSystemImportsGroupDocs.Conversion.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply the license
License.Set(publicKey,privateKey)' Create a converter from file path
DimconverterAsNewHtmlToPdfConverter("sample.html")' Convert HTML to PDF
converter.Convert("sample.pdf")EndSubEndModule
openSystemopenGroupDocs.Conversion.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply the license
License.Set(publicKey,privateKey)// Create a converter from file path
letconverter=newHtmlToPdfConverter("sample.html")// Convert HTML to PDF
converter.Convert("sample.pdf")0
This section covers only the main scenarios. You can also refer to the API references for the WebLoadOptions for a complete list of options that you can specify.
Convert HTML with Custom CSS Styling
The following example shows how to apply custom CSS styling when converting HTML to PDF using the CustomCssStyle property.
usingSystem;usingGroupDocs.Conversion.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply licenseLicense.Set(publicKey,privateKey);// Apply custom CSS styling through load optionsvarconverter=newHtmlToPdfConverter("sample.html",options=>{options.CustomCssStyle="body { color: coral !important; }";});// Convert HTML to PDFconverter.Convert("styled-sample.pdf");
ImportsGroupDocs.Conversion.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply license
License.Set(publicKey,privateKey)' Apply custom CSS styling through load options
DimconverterAsNewHtmlToPdfConverter("sample.html",Sub(options)options.CustomCssStyle="body { font-family: Arial, sans-serif; font-size: 14px; color: #333; }"EndSub)' Convert HTML to PDF
converter.Convert("styled-sample.pdf")EndSubEndModule
openSystemopenGroupDocs.Conversion.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply license
License.Set(publicKey,privateKey)// Apply custom CSS styling through load options
letconverter=newHtmlToPdfConverter("sample.html",funoptions->options.CustomCssStyle<-"body { font-family: Arial, sans-serif; font-size: 14px; color: #333; }")// Convert HTML to PDF
converter.Convert("styled-sample.pdf")0
The following example shows how to set a custom zoom level when converting HTML to PDF using the Zoom property.
Note: When using zoom levels greater than 100%, the content may not fit into standard page sizes. Consider using PageLayoutOptions to fit page by width or height if needed.
usingSystem;usingGroupDocs.Conversion.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply licenseLicense.Set(publicKey,privateKey);// Set zoom level to 150% through load optionsvarconverter=newHtmlToPdfConverter("sample.html",options=>{options.Zoom=150;// 150% zoom level});// Convert HTML to PDFconverter.Convert("zoomed-sample.pdf");
ImportsGroupDocs.Conversion.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply license
License.Set(publicKey,privateKey)' Set zoom level to 150% through load options
DimconverterAsNewHtmlToPdfConverter("sample.html",Sub(options)options.Zoom=150' 150% zoom level
EndSub)' Convert HTML to PDF
converter.Convert("zoomed-sample.pdf")EndSubEndModule
openSystemopenGroupDocs.Conversion.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply license
License.Set(publicKey,privateKey)// Set zoom level to 150% through load options
letconverter=newHtmlToPdfConverter("sample.html",funoptions->options.Zoom<-150// 150% zoom level
)// Convert HTML to PDF
converter.Convert("zoomed-sample.pdf")0
The following example shows how to skip external resources when converting HTML to PDF using the SkipExternalResources property.
usingSystem;usingGroupDocs.Conversion.LowCode;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply licenseLicense.Set(publicKey,privateKey);// Skip external resources through load optionsvarconverter=newHtmlToPdfConverter("with-image.html",options=>{options.SkipExternalResources=true;});// Convert HTML to PDFconverter.Convert("without-image.pdf");
ImportsGroupDocs.Conversion.LowCodeModuleProgramSubMain()' Load license keys
DimpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply license
License.Set(publicKey,privateKey)' Skip external resources through load options
DimconverterAsNewHtmlToPdfConverter("with-image.html",Sub(options)options.SkipExternalResources=TrueEndSub)' Convert HTML to PDF
converter.Convert("without-image.pdf")EndSubEndModule
openSystemopenGroupDocs.Conversion.LowCode[<EntryPoint>]letmain_=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply license
License.Set(publicKey,privateKey)// Skip external resources through load options
letconverter=newHtmlToPdfConverter("with-image.html",funoptions->options.SkipExternalResources<-true)// Convert HTML to PDF
converter.Convert("without-image.pdf")0
The examples in this section shows how you can adjust the output using PdfConvertOptions.
Convert HTML to PDF with Specific PDF Format
You can specify the PDF format for the output file using the PdfFormat property in PdfOptions class. This allows you to create PDF files that conform to specific standards like PDF/A for archiving or PDF/X for print production.
The following example shows how to convert an HTML file to PDF/A-1b format, which is commonly used for long-term archiving:
usingSystem;usingGroupDocs.Conversion.LowCode;usingGroupDocs.Conversion.Options.Convert;// Load license keysvarpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY");varprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY");// Apply licenseLicense.Set(publicKey,privateKey);// Create the convertervarconverter=newHtmlToPdfConverter("sample.html");// Convert to PDF/A-1b format for archivingconverter.Convert("archived-sample.pdf",convertOptions=>{convertOptions.PdfOptions.PdfFormat=PdfFormats.PdfA_1B;});
ImportsGroupDocs.Conversion.LowCodeImportsGroupDocs.Conversion.Options.ConvertModuleProgramSubMain()' Load license keys
DimpublicKeyAsString=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")DimprivateKeyAsString=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")' Apply license
License.Set(publicKey,privateKey)' Create the converter
DimconverterAsNewHtmlToPdfConverter("sample.html")' Convert to PDF/A-1b format for archiving
converter.Convert("archived-sample.pdf",Sub(convertOptions)convertOptions.PdfOptions.PdfFormat=PdfFormats.PdfA_1BEndSub)EndSubEndModule
openSystemopenGroupDocs.Conversion.LowCodeopenGroupDocs.Conversion.Options.Convert[<EntryPoint>]letmainargv=// Load license keys
letpublicKey=Environment.GetEnvironmentVariable("GD_PUBLIC_KEY")letprivateKey=Environment.GetEnvironmentVariable("GD_PRIVATE_KEY")// Apply license
License.Set(publicKey,privateKey)// Create the converter
letconverter=newHtmlToPdfConverter("sample.html")// Convert to PDF/A-1b format for archiving
converter.Convert("archived-sample.pdf",funconvertOptions->convertOptions.PdfOptions.PdfFormat<-PdfFormats.PdfA_1B)0//returnexitcode