Converting HTML to PDF via API not working

Hi,

I’m trying to send the contents of a web page to the service and have it render me a pdf stream back…I keep getting 400 errors bad request…I’m using the SDK to handle most of the work…below is a snippet of the code…just sending in a simple html snippet…any thoughts? It’s sending in the content type as json…
Thanks
Doug

BaseURI = http://api.saaspose.com/v1.0

public byte[] Convert(string url)
{
var urlData = GetDataString(url);
var converter = new Converter();
Stream pdfStream = converter.ConvertLocalFile(urlData, SaveFormat.PDF);
return StreamConvert(pdfStream);
}

private string GetDataString(string url)
{
return @“Custom PDF Test<style type=”“text/css”“>h1{ text-align: center }

Custom PDF Test page

”;
}

public static Stream ProcessCommand(string strURI, string strHttpCommand, string strContent)
{
try
{
byte[] arr = Encoding.UTF8.GetBytes(strContent);

var address = new Uri(strURI);
var request = (HttpWebRequest) WebRequest.Create(address);
request.Method = strHttpCommand;
request.ContentType = “application/json”;

request.ContentLength = arr.Length;

Stream dataStream = request.GetRequestStream();
dataStream.Write(arr, 0, arr.Length);
dataStream.Close();

var response = (HttpWebResponse) request.GetResponse();
return response.GetResponseStream();
}
catch (WebException webex)
{
throw new Exception(webex.Message);
}
catch (Exception Ex)
{
throw new Exception(Ex.Message);
}
}

Hi Doug,

Document class supports creating PDF from HTML. Please check Create PDF from HTML|Documentation for more details.

You can also use Aspose.Words for Cloud to convert HTML to PDF. Please check Convert Documents|Aspose Words Cloud Docs for more details. Just pass HTML file as input and specify PDF as output format using this example.

Please feel free to contact us in case you have further comments or questions.

Best Regards,

Thanks for your reply. Your links take me to converting an existing word document to pdf format. My need is different. For any given URL I am getting the HTML bytes in a memory stream and then calling your method to process them into PDF. I am getting 400 errors.


As an example if I wanted to take the content from http://www.mayoclinic.com/health/asthma/DS00021 and create a good looking PDF from it why does my example fail? I’m getting 400 errors which from what I can tell is that the API doesn’t like the format. Does it have to be a filestream? A stream is a stream right?

Regards
Doug

Hi Doug,

I saved the mentioned HTML to disk and used the following code to convert it to PDF:

using (FileStream file = new FileStream(@"d:\Asthma - MayoClinic.com.html", FileMode.Open, FileAccess.Read))
{
    AsposeApp.AppSID = "77*************";
    AsposeApp.AppKey = "***********"    ;

    string strURI = "http://api.aspose.com/v1.1/words/convert?format=pdf";

    string signedURI = Utils.Sign(strURI);
    ;
    ;
    Stream responseStream = Utils.ProcessCommand(signedURI, "PUT", file);

    using (Stream fileStream = System.IO.File.OpenWrite(@"d:\Asthma - MayoClinic.com.pdf"))
    {
        Utils.CopyStream(responseStream, fileStream);
    }
    ;

    responseStream.Close();

}

You can change the ProcessCommand method according to your requirement and accept memory stream. Please let us know if you find any difficulty in doing so, and we will be glad to alter this method for you.

Best Regards,

Hi Aspose Team,


The above code works (converting from word to PDF)

Can you provide code to convert from HTML to PDF via API call like you did above.

This would be very helpful.

Regards
Trevor

Hi Trevor,


Thanks for your inquriy. Please check following documentation link for updated code for HTML to PDF conversion using Aspose.Pdf for Clouds with REST API. Hopefully it will help you to accomplish the task. However if you face some issue then please share your source document and error details, so we will look into it and will guide you accordingly.


Best Regards,