We're sorry AsposeCloud doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.cloud

Convert HTML to PDF in C# using Aspose.PDF REST API throws authorization error

My goal is to create a pdf from some html. I read the following documentation and none of it seems to work:

Github samples:

The sample code shows method calls and classes that don’t exist. In the readme the Configuration class can’t be instantiated that way (code from readme posted below) nor does it have ClientId and ClientSecret properties. It does have ApiKey and ApiSid are those the same as the ones in the portal that say Client Id and Secret?

var config = new Configuration
	{
		ClientId = "MY_CLIENT_ID",
		ClientSecret = "MY_CLIENT_SECRET",
		ApiBaseUrl = baseUrl,
	};

	var pdfApi = new PdfApi(config);

In the c# sdk sample located here ( https://docs.aspose.cloud/pdf/create-pdf-from-html/#inssdk-examples ) the method signature for PutCreateDocument(fileName, templateFile, dataFile, templateType, storage, folder); doesn’t exist either.

My question is how is one supposed to upload an HTML document and convert it to PDF. None of the samples posted seem correct. The error I am getting with what I think are the correct changes is:

ClientId is undefined. Please check authorization.","Description":"Operation Failed. General

@forbattring

We are sorry for the inconvenience. We recently introduced API Version 3.0 and there are some breaking changes in it. Now it includes its own storage API methods. We are in the process to update the documentation. Meanwhile, you can check the unit tests for sample code from the GitHub repository.

Please check the following sample code to upload a HTML file to storage, convert it to PDF and download the output file.

// Get Client ID and Client Secret from https://dashboard.aspose.cloud/
PdfApi pdfApi = new PdfApi(ClientSecret, ClientID);
string name = "original.html";
string resultName = "original.pdf";
string localFolder = "C:/Temp/";
int height = 850;
int width = 600;
int top_margin = 10;
int bottom_margin = 10;
int right_margin = 10;
int left_margin = 10;

          
// Upload a File to Storage
using (var file = System.IO.File.OpenRead(localFolder+name))
{
    var uploadResponse = pdfApi.UploadFile(name, file);

}
// Convert HTML file to PDF
var convertResponse = pdfApi.PutHtmlInStorageToPdf(name:resultName, srcPath: name, height:height, width:width, marginLeft: left_margin, marginBottom: bottom_margin, marginRight: right_margin, marginTop: top_margin);

// Download a File from Storage
var downloadResponse = pdfApi.DownloadFile(resultName);
var fileStream = System.IO.File.Create("C:/Temp/original.pdf");
downloadResponse.CopyTo(fileStream);
fileStream.Close();