QuickStart

It is quite easy to use Aspose.Html for Cloud in your projects. All you need to do is:

1. Create a Free Account

Please go to https://dashboard.aspose.cloud/#/ to create a free account.

2. Create New App and Get App Key and SID

Before you can make any requests to Aspose for Cloud APIs you need to create an API client app. This will give you App SID and App key (secret key) which you can use to invoke Aspose for Cloud APIs.

3. Install the SDK of your choice

Aspose Html for Cloud SDK is written in 2 different languages, all you need to get started is to add our SDK to your existing project.


// Install Aspose.Html Cloud SDK via NuGet

PM> Install-Package Aspose.Html-Cloud              


pip install -r Aspose.Html-Cloud-SDK-for-Python/requirements.txt
python Aspose.Html-Cloud-SDK-for-Python/setup.py install


To install the API client library to your local Maven repository, simply execute:

mvn install


Composer:

To install the bindings via Composer, add the following to composer.json:

{
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/aspose-html-cloud/aspose-html-cloud-php.git"
    }
  ],
  "require": {
    "aspose-html-cloud/aspose-html-cloud-php": "\*@dev"
  }
}

Then run composer install

Manual Installation:



Download the files and include autoload.php:

require_once('/path/to/aspose-html-cloud-php/vendor/autoload.php');



bundle install --jobs 4

Build a gem:

To build the Ruby code into a gem:

gem build aspose_html.gemspec

Then either install the gem locally:

gem install ./aspose_html-1.0.0.gem


,To use the library locally without publishing to a remote npm registry, first install the dependencies by changing into the directory containing package.json (and this README). Let's call this JAVASCRIPT_CLIENT_DIR. Then run:

npm install

Next, link it globally in npm with the following, also from JAVASCRIPT_CLIENT_DIR:

npm link

Finally, switch to the directory you want to use your asposehtmlcloud from, and run:

npm link /path/to/<JAVASCRIPT_CLIENT_DIR>

You should now be able to require('asposehtmlcloud') in javascript files from the directory you ran the last command above from.

Loading from GIT



git clone https://github.com/aspose-html-cloud/aspose-html-cloud-cpp.git
cd aspose-html-cloud-cpp


pip install -r Aspose.Html-Cloud-SDK-for-Python/requirements.txt
python Aspose.Html-Cloud-SDK-for-Python/setup.py install


Clone the GitHub repository and import the project into your workspace


Building the API client library requires Gradle Build Tool to be installed.
To build the API client library, simply execute:

gradlew.bat

4. Make an API request from the SDK of your choice

Use the App SID and App key (secret key) from the API App Client you created in step one and replace in the corresponding code. Below is an example of translating HTML page from English to German, French and Russian languages.

HTML to PDF Conversion


using System;
using System.IO;
using Aspose.Html.Cloud.Sdk.Api;
using Aspose.Html.Cloud.Sdk.Api.Model;
using Aspose.Html.Cloud.Sdk.Api.Interfaces;
using Aspose.HTML.Cloud.Examples.SDK;

namespace Aspose.Html.Cloud.Examples
{
    class Program
    {
        const string appSID = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; // apply user's appSID
        const string appKey = "xxxxxxxxxxxxxxxxxxxxxxxxx";           // apply user's key
        const string SERVICE_API_HOST = "https://api.aspose.cloud";
        const string SERVICE_AUTH_HOST = "https://api.aspose.cloud";
        const string LOCAL_DATA_FOLDER = @ "d:\testdata";
        const string STORAGE_DATA_FOLDER = "Html/TestData";
        const string LOCAL_RESULT_FOLDER = @ "d:\testout";

        static void Main(string[] args)
                {
            // setup HTML document name

            var name = "testpage4_embcss.html";

            // setup local document path

            var srcPath = Path.Combine(LOCAL_DATA_FOLDER, name);

            // setup storage folder where the source document should be present

            string folder = STORAGE_DATA_FOLDER;

            // setup storage name (null for default storage)

            string storage = null;

            // setup resulting file parameters

            int width = 800;
            int height = 1200;
            int leftMargin = 15;
            int rightMargin = 15;
            int topMargin = 15;
            int bottomMargin = 15;

            try
            {
                var storagePath = Path.Combine(STORAGE_DATA_FOLDER, name).Replace('\\', '/');
                UploadToStorage(srcPath, storagePath);
                IConversionApi convApi = new HtmlApi(appSID, appKey, SERVICE_API_HOST);
                var response = convApi.GetConvertDocumentToPdf(
                    name, width, height, leftMargin, rightMargin, topMargin, bottomMargin, folder, storage);
                if (response != null && response.ContentStream != null && response.Status == "OK")
                {
                    Console.Out.WriteLine("Converted successfully");
                    // save result file
                    var respFileName = response.FileName;
                    Stream outStream = response.ContentStream;
                    var outPath = Path.Combine(LOCAL_RESULT_FOLDER, respFileName);
                    using(FileStream fstr = new FileStream(outPath, FileMode.Create, FileAccess.Write))
                    {
                         outStream.Position = 0;
                         outStream.CopyTo(fstr);
                         fstr.Flush();
                         Console.WriteLine(string.Format("\nResult file downloaded to: {0}", outPath));
                    }
                }
                                else
                    throw new Exception($ "Operation failed: status {response.Status}");
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(string.Format("Error: {0}", ex.Message));
            }
            Console.Out.WriteLine("Press any key to end:");
            Console.In.Read();
        }


        static void UploadToStorage(string localPath, string storagePath, string storage)
        {
            var name = Path.GetFileName(localPath);
            Configuration storageConf = new Configuration() {
            ApiBaseUrl = SERVICE_API_HOST,
            AppKey = appKey,
            AppSid = appSID,
            AuthUrl = SERVICE_AUTH_HOST,
            ApiVersion = "3.0"
            };

            StorageApi storageApi = new StorageApi(storageConf);
            
            try
            {
                            using(Stream fstr = new FileStream(localPath, FileMode.Open, FileAccess.Read)) {
                var response = storageApi.UploadFile(fstr, storagePath, storage);
                if (response.Code == 200)
                {
                    Console.Out.WriteLine($ "File {name} successfully uploaded with path {storagePath} .");
                }

                else
                    throw new Exception($ "File Upload failed: status {response.Status}");
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine(ex.Message);
                throw;
            }
        }
    }
}


from __future__ import absolute_import 
from __future__ import print_function 
from pprint import pprint  
import time  



import os 
from shutil import copy2
from asposehtmlcloud.configuration import Configuration
from asposehtmlcloud.api_client import ApiClient as Client
from asposehtmlcloud.api.html_api import HtmlApi
from asposehtmlcloud.api.storage_api import StorageApi
from asposehtmlcloud.rest import ApiException

class Program:

    @classmethod
    def set_configuration(cls):
        cls.config = Configuration(
            apiKey="xxxxxxxxxxxxxxxxxxxxxxxxx",
            appSid="xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            basePath="https://api.aspose.cloud",
            authPath="https://api.aspose.cloud",
            debug=True)

    @classmethod
    def move_file(cls, src_file, dst_file):
        if os.path.isfile(src_file):
            copy2(src_file, dst_file)
            os.remove(src_file)

    def __init__(self):
        set_configuration()
        self._client = Client(Program.config)
        self._html_api = HtmlApi(self._client)
        self._storage_api = StorageApi(self._client)

    def upload_to_storage(self, localPath: str, storagePath: str, storage: str = None) -> bool:

        api = self._storage_api
        result = api.upload_file(storagePath, localPath)
        return len(result.uploaded) == 1 and len(result.errors) == 0

    def run_get_convert_html_to_pdf(): 

        name = "testpage1.html"
        folder = "/Html/TestData"
        storage = None

        localFolder = "d:\\testdata"
        destFolder = 'd:\\testresult'

        localPath = "{0}\\{1}".format(localFolder, name)
        storagePath = "{0}/{1}".format(folder, name)

        if self.upload_to_storage(localPath, storagePath, storage):
            api = self._html_api
            try:
                res = api.get_convert_document_to_pdf(
                    name, width=800, height=1000, left_margin=50, right_margin=100, top_margin=150, bottom_margin=200,
                    folder=TestHelper.folder, storage="" )
                move_file(str(res), destFolder)

            except ApiException as e:
                print("Operation failed: {0}".format(str(e)))

        else:
            print("Error uploading file to storage: ")
            return

if __name__ == '__main__':

    Program().run_get_convert_html_to_pdf()