HTML to PDF

Convert HTML to PDF using .NET REST API.

The ability to convert HTML content to PDF format has become indispensable for various businesses and developers. Whether we are saving webpages as PDFs for archival purposes, generating reports, or sharing web content with users, HTML to PDF conversion plays a vital role. This powerful feature allows you to preserve the structure, styling, and interactivity of web content while transforming it into a universally accessible and printable format.

In this article, we’ll explore the multitude of benefits that come with the usage of Aspose.PDF Cloud SDK for HTML to PDF conversion. We are going to learn how it empowers you to enhance your document processing capabilities, streamline workflows, and deliver an exceptional user experience.

REST API for HTML to PDF Conversion

Aspose.PDF Cloud SDK for .NET offers comprehensive and powerful features for HTML to PDF conversion, making it a top choice for developers seeking a reliable and efficient document processing solution. With this SDK, you can seamlessly convert HTML content to high-quality PDF documents while preserving the layout, styles, and hyperlinks. This enables you to create visually appealing and interactive PDF files directly from HTML sources, making it ideal for generating reports, invoices, product catalogs, and more.

Now, to begin using this approach, we need to first add the SDK reference in our project. Therefore, please search Aspose.PDF-Cloud in NuGet packages manager inside Visual Studio IDE and click the Add Package button. You also need to obtain your client credentials from cloud dashboard. In case you do not have an existing account, simply create a free account by following the instructions specified over quick start.

Webpage to PDF using C# .NET

Please try using the following code snippet to accomplish the requirement to convert webpage to PDF.

HTML to PDF

HTML to PDF conversion preview.

Given below are the details regarding above stated code snippet.

PdfApi pdfApi = new PdfApi(clientSecret, clientID);

Firstly, create an object of PdfApi class while passing client credentials as input arguments.

var result = pdfApi.GetHtmlInStorageToPdf("converted.zip", htmlFileName: "converted6.html", 
        height: 1000, width: 800, isLandscape: false,
        marginLeft: 10, marginRight: 10, marginTop: 10, marginBottom: 10);

Call the API to convert HTML to PDF. As an argument, we pass the name of .zip archive containing the HTML file, the name of the HTML file to be converted, the resultant PDF dimensions and margin details.

saveToDisk(result, "/Users/nayyer/Downloads/htmlOutput.pdf");

Finally, we call the method to save the resultant PDF to a local drive.

In case we need to perform the conversion and save the resultant file to cloud storage, please call the API PutHtmlInStorageToPdf.

HTML to PDF Online using cURL Commands

Converting HTML to PDF using cURL commands in combination with Aspose.PDF Cloud offers a convenient and versatile approach for developers looking to perform document conversion programmatically. Aspose.PDF Cloud provides a RESTful API that allows you to integrate the HTML to PDF conversion functionality into your applications using simple HTTP requests. This method is particularly beneficial for those who prefer working with command-line tools or require flexibility in integrating the conversion feature into different environments.

The first step in this approach is the generation of a JWT access token. So, please execute the following command:

curl -v "https://api.aspose.cloud/connect/token" \
 -X POST \
 -d "grant_type=client_credentials&client_id=bb959721-5780-4be6-be35-ff5c3a6aa4a2&client_secret=4d84d5f6584160cbd91dba1fe145db14" \
 -H "Content-Type: application/x-www-form-urlencoded" \
 -H "Accept: application/json"

Once the JWT token has been generated, please execute the following command to convert the HTML file available in cloud storage to PDF format and upload the resultant output to the same cloud storage:

curl -v "https://api.aspose.cloud/v3.0/pdf/{resultantFile}/create/html?srcPath=converted.zip&htmlFileName={sourceHTML}" \
-X PUT \
-H  "accept: application/json" \
-H  "authorization: Bearer {accessToken}" \
-d {} -v

Replace sourceHTML with the name of input HTML document available in cloud storage, and resultantFile with the name of resultant PDF document to be stored in the cloud storage.

In case we have a requirement to convert the HTML file to PDF format and save the output on local drive, please execute the following command:

curl -v "https://api.aspose.cloud/v3.0/pdf/create/html?srcPath=sourceFolder.zip&htmlFileName=source.html&height=1000&width=800&isLandscape=false&marginLeft=10&marginBottom=10&marginRight=10&marginTop=10" \
-X GET \
-H "accept: multipart/form-data" \
-H "authorization: Bearer {accessToken}" \
-o "Converted.pdf"

You may consider downloading the sample files used in the above example from inputHTML.html and htmlOutput.pdf.

Conclusion

In conclusion, converting HTML to PDF is a fundamental requirement in various industries, and by leveraging Aspose.PDF Cloud SDK for .NET, along with cURL commands, we get a powerful and efficient solution to achieve this task. Furthermore, with Aspose.PDF Cloud’s robust features, such as layout preservation and customizable parameters, you can confidently generate high-quality PDF documents from HTML content. Whether you’re building dynamic web pages, generating reports, archiving data, or creating printable documents, this approach ensures consistent and accurate conversion results.

We highly recommend visiting the following blogs: