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()