1
Answer

C#,Java & Ruby Code Samples to Create PDF File from HTML

Jumaila Shahzad

Jumaila Shahzad

10y
1.1k
1

This tutorial shows how developers can create PDF file from HTML in Cloud using Aspose.Pdf for Cloud API in their applications. You can use this REST API with any language: .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more.


[C# Code Sample]

stringstrURI = "http://api.aspose.com/v1.1/pdf/outPdfFile.pdf?templateFile=input.html&templateType=html";

stringsignedURI = Sign(strURI);

StreamReader reader = new StreamReader(ProcessCommand(signedURI, "PUT"));

//further process JSON response

stringstrJSON = reader.ReadToEnd();

//Parse the json string to JObject

JObjectparsedJSON = JObject.Parse(strJSON);

BaseResponse stream = JsonConvert.DeserializeObject<BaseResponse>(parsedJSON.ToString());

if (stream.Code == "200" &&stream.Status == "OK")

Console.WriteLine("Empty PDF file has been created successfully");

 

//Here is the BaseResponse class

public class BaseResponse

{

publicBaseResponse() { }

        public string Code { get; set; }

        public string Status { get; set; }

}

 

 

[Java Code Sample]

 

 

//build uri to create empty pdf

    String strURI = "http://api.aspose.com/v1.1/pdf/outPdfFile.pdf?templateFile=input.html&templateType=html";

    String signedURI = Sign(strURI);

InputStreamresponseStream = ProcessCommand(signedURI, "PUT");

    String strJSON = StreamToString(responseStream);

Gsongson = new Gson();

    //Parse the json string to JObject and Deserializes the JSON to a object.

BaseResponsebaseResponse = gson.fromJson(strJSON,BaseResponse.class);

if (baseResponse.getCode().equals("200") &&baseResponse.getStatus().equals("OK"))

System.out.println("Empty PDF file has been created successfully");

 

//Here is the BaseResponse class

public class BaseResponse {

        publicBaseResponse() { }

        private String Code;

        private String Status;

        public String getCode(){return Code;}

        public String getStatus(){return Status;}

        public void  setCode(String temCode){ Code=temCode;}

        public void setStatus(String temStatus){ Status=temStatus;}

}

 

 

[Ruby Code Sample]

 

 

app_sid = '77******-1***-4***-a***-8***********'

app_key = '89******************************'

Aspose::Cloud::Common::AsposeApp.new(app_sid, app_key)

 

#build URI to create PDF from HTML

str_uri = 'http://api.aspose.com/v1.1/pdf/outPdfFile.pdf?templateFile=HtmlExample1.html&templateType=html'

 

#sign URI

signed_uri = Aspose::Cloud::Common::Utils.sign(str_uri)

RestClient.put(signed_uri, '', {:accept=>'application/json'})

 

#build URI to download output file

str_uri = 'http://api.aspose.com/v1.1/storage/file/outPdfFile.pdf'

 

#sign URI

signed_uri = Aspose::Cloud::Common::Utils.sign(str_uri)

response_stream = RestClient.get(signed_uri, :accept => 'application/json')

Aspose::Cloud::Common::Utils.save_file(response_stream, "outPdfFile.pdf")

p 'done'

 

Answers (1)