Convert HTML to MHTML – Aspose.HTML Cloud SDK Converting between formats is required for various reasons: to work in a familiar, convenient format or to take advantage of different formats for specific tasks. Aspose.HTML Cloud provides the simplest way to convert HTML documents to MHTML format with high quality, easy and fast.
MHTML combines HTML and its associated resources (images, stylesheets, and scripts) into a single file. HTML to MHTML conversion makes it easier to archive and share web content by combining all related resources into one self-contained file. Moreover, HTML to MHTML conversion offers benefits such as offline browsing, saving web content, email attachments, regulatory compliance, etc.
This article contains code examples explaining how to convert HTML to MHTML using available SDKs. We will consider various HTML to MHTML conversion scenarios: from storage to storage, a local file to a local file system, and from the Web to the local file system.
Examples of HTML to MHTML Conversion Aspose.HTML Cloud allows you to fetch an HTML document from a storage location by its name, from a URL or a local file on your drive, convert it to the specified format and save it to the storage or the local drive. The following code examples demonstrate how to convert HTML to MHTML programmatically for different cases.
Example 1. Convert HTML to MHTML with default save options Consider an example of converting a local HTML file to MHTML and saving the result to a local path.
C# The following example demonstrates the simplest way to convert HTML to MHTML using C#. You can download the C# SDK from the
GitHub repository .
Copy 1 // Initialize SDK API
2 var api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" ). ConvertApi ;
3
4 // Convert from HTML file to MHTML file
5 var result = await api. ConvertAsync ( "test.html" , "test.mhtml" );
Java The following example demonstrates how to convert HTML to MHTML using Java. You can download the java SDK from the
GitHub repository .
Copy 1 Configuration. setBasePath ( "https://api.aspose.cloud" );
2 Configuration. setAuthPath ( "https://api.aspose.cloud/connect/token" );
3
4 HtmlApi api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" );
5
6 JobBuilder builder = new ConverterBuilder()
7 . fromLocalFile ( "input.html" )
8 . saveToLocal ( "output.mhtml" );
9
10 OperationResult result = api. convert ( builder);
C++ The following example demonstrates how to convert HTML to MHTML C++ language applying. Local HTML converted to MHTML and saved to the local path.
Copy 1 // Get current directory
2 std: : string cur_dir ( argv[ 0 ]);
3 int pos = cur_dir. find_last_of ( "/\\" );
4 cur_dir = cur_dir. substr ( 0 , pos + 1 ); // Include the last slash
5 std: : wstring w_cur_dir ( cur_dir. begin (), cur_dir. end ());
6
7 const utility:: string_t clientId = L"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ;
8 const utility:: string_t clientSecret = L"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ;
9 const utility:: string_t basePath = L"https://api.aspose.cloud/v4.0" ;
10 const utility:: string_t authPath = L"https://api.aspose.cloud/connect/token" ;
11
12 // Create configuration for authorization
13 std: : shared_ptr< ApiConfiguration> apiConfig ( new ApiConfiguration( clientId, clientSecret, basePath, authPath));
14
15 // Create client from configuration
16 std: : shared_ptr< ApiClient> apiClient ( new ApiClient( apiConfig));
17
18 // Create ConversionApi
19 std: : shared_ptr< ConversionApi> api = std:: make_shared< ConversionApi>( apiClient);
20
21 // File name for conversion
22 utility: : string_t src = w_cur_dir + L"test.html" ;
23 utility: : string_t dst = w_cur_dir + L"result.mhtml" ;
24
25 //Conversion
26 auto result = api-> convertLocalToLocal( src, dst);
27
28 // Check the result file
29 auto re = result-> getFile();
30 std: : ifstream f ( re. c_str ());
31 if (! f. good ())
32 {
33 throw std:: runtime_error( "Conversion failed" );
34 }
Python The following example demonstrates how to convert HTML to MHTML Python language applying. You can download the Python SDK from the
GitHub repository .
Copy 1 from asposehtmlcloud. configuration import Configuration
2 from asposehtmlcloud. api . html_api import HtmlApi
3 from asposehtmlcloud. api_client import ApiClient as Client
4 from asposehtmlcloud. rest import ApiException
5
6 configuration = Configuration( apiKey= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
7 appSid= "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
8 basePath= "https://api.aspose.cloud/v4.0" ,
9 authPath= "https://api.aspose.cloud/connect/token" ,
10 debug= True)
11 client = Client( configuration)
12 html_api = HtmlApi( client)
13
14 try:
15 res = html_api. convertApi . convert_local_to_local ( input_file= "test.html" , output_file= "test.mhtml" )
16 except ApiException as ex:
17 print( "Exception" )
18 print( "Info: " + str( ex))
19 raise ex
PHP The following example demonstrates how to convert HTML to MHTML using PHP. You can download the PHP SDK from the
GitHub repository
Copy 1 <? php
2 require_once ( __DIR__ . ' / vendor/ autoload. php ' );
3
4 $conf = array(
5 "basePath" => "https://api.aspose.cloud/v4.0" ,
6 "authPath" => "https://api.aspose.cloud/connect/token" ,
7 "apiKey" => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
8 "appSID" => "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
9 "defaultUserAgent" => "Webkit"
10 );
11
12 $api_html = new Client\ Invoker\ Api\ HtmlApi( $conf);
13
14 $src = ' input. html ' ;
15 $dst = ' output. mhtml ' ;
16
17 try {
18 // Request to server Api
19 $result = $api_html-> convertLocalToLocal( $src, $dst);
20 print_r( $result);
21 } catch ( Exception $e) {
22 echo ' Exception when calling HtmlApi-> convertLocalToLocal: ' , $e-> getMessage(), PHP_EOL;
23 }
24
25 ?>
Ruby The following example demonstrates how to convert HTML to MHTML Ruby language applying. You can download the Ruby SDK from the
GitHub repository .
Copy 1 # load the gem
2 require ' aspose_html_cloud'
3
4 # Get keys from aspose site.
5 # There is free quota available.
6 # For more details, see https: //purchase.aspose.cloud/pricing
7
8 CONFIG = {
9 "basePath" : "https://api.aspose.cloud/v4.0" ,
10 "authPath" : "https://api.aspose.cloud/connect/token" ,
11 "apiKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
12 "appSID" : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
13 "debug" : true
14 }
15
16 api_instance = AsposeHtml:: HtmlApi. new CONFIG
17
18 src = "test.html" # String | Full path to the input file.
19 dst = "test.mhtml" # String | Full path to the result.
20
21 begin
22 # Convert the document from the local file and save result to the local file.
23 result = api_instance. convert_local_to_local ( src, dst)
24 p result
25 rescue AsposeHtml:: ApiError => e
26 puts "Exception when calling api_instance.convert_local_to_local: #{e}"
27 end
Node.js The following example demonstrates how to convert HTML to MHTML Node.js language applying. Local HTML converted to MHTML and saved to the local path.
Copy 1 // Get keys from aspose site.
2 // There is free quota available.
3 // For more details, see https://purchase.aspose.cloud/pricing
4
5 var conf = {
6 "basePath" : "https://api.aspose.cloud/v4.0" ,
7 "authPath" : "https://api.aspose.cloud/connect/token" ,
8 "apiKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
9 "appSID" : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
10 "defaultUserAgent" : "NodeJsWebkit"
11 };
12
13 var api = require( ' @asposecloud / aspose- html- cloud' );
14
15 // Create Conversion Api object
16 var conversionApi = new api. ConversionApi ( conf);
17
18 var src = "/path/to/src/test.html" ; // {String} Source document.
19 var dst = "/path/to/dst/test.mhtml" ; // {String} Result document.
20 var opts = null ;
21
22 var callback = function( error, data, response) {
23 if ( error) {
24 console. error ( error);
25 } else {
26 console. log ( data);
27 }
28 };
29
30 conversionApi. convertLocalToLocal ( src, dst, opts, callback);
Swift The following example demonstrates how to convert HTML to MHTML Swift language applying. You can download the Swift SDK from the
GitHub repository .
Copy 1 import Alamofire
2 import Foundation
3 import XCTest
4 import AsposeHtmlCloud
5
6 static let fm = FileManager. default
7 let resourceDir = fm. homeDirectoryForCurrentUser . appendingPathComponent ( "Documents/Aspose.HTML.Cloud.SDK.Swift/Tests/AsposeHtmlCloudTests/Resources" )
8 let resultDir = fm. homeDirectoryForCurrentUser . appendingPathComponent ( "Documents/Aspose.HTML.Cloud.SDK.Swift/Tests/AsposeHtmlCloudTests/TestResult" )
9
10 func url ( forResource fileName: String) -> URL {
11 return resourceDir. appendingPathComponent ( fileName)
12 }
13
14 func fileExist ( name: String) -> Bool {
15 return FileManager. default . fileExists ( atPath: name)
16 }
17
18 ClientAPI. setConfig (
19 basePath: "https://api.aspose.cloud/v4.0" ,
20 authPath: "https://api.aspose.cloud/connect/token" ,
21 apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
22 appSID: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
23 debugging: true
24 )
25
26 let fileName = "test.html"
27 let format = "mhtml"
28 let src = url( forResource: fileName). absoluteString
29
30 let expectation = self. expectation ( description: "testConvert to \(format)" )
31 let dst = resultDir. appendingPathComponent ( "Output.\(format)" ). absoluteString
32 HtmlAPI. convertLocalToLocal ( src: src, dst: dst, options: nil) { ( data, error) in
33
34 guard error == nil else {
35 XCTFail( "Error convert html to \(format)). Error=\(error!.localizedDescription)" )
36 return
37 }
38 let resultPath = data!. file !
39 XCTAssertTrue( fileExist( name: resultPath))
40 expectation. fulfill ()
41 }
42 self. waitForExpectations ( timeout: 100.0 , handler: nil)
Java/Android The following example demonstrates how to convert HTML to MHTML using Java/Android. You can download the Java/Android SDK from the
GitHub repository .
Copy 1 Configuration. setBasePath ( "https://api.aspose.cloud" );
2 Configuration. setAuthPath ( "https://api.aspose.cloud/connect/token" );
3
4 HtmlApi api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" );
5
6 JobBuilder builder = new ConverterBuilder()
7 . fromLocalFile ( "input.html" )
8 . saveToLocal ( "output.mhtml" );
9
10 OperationResult result = api. convert ( builder);
CURL The following example demonstrates how to convert HTML to MHTML using REST API. Follow a few required steps:
Upload local file to storage using
Storage API . Call REST API to execute conversion (in the example below). Download conversion result back from storage using
Storage API . Copy 1 curl - X POST - v \
2 "https://api.aspose.cloud/v4.0/html/conversion/html-mhtml" \
3 - d "{'InputPath': '/testpage.html', 'OutputFile': 'test.mhtml'}" \
4 - H "Content-Type: application/json" \
5 - H "Authorization:Bearer <JWT_token>"
Example 2. Convert a Web page to MHTML Converting to MHTML helps maintain the integrity and structure of web pages, ensuring that all elements are preserved for future use or redistribution. Aspose.HTML Cloud allows you to get a web page by its URL, convert it to another format and save into a local file system. The example below demonstrates how to convert a web page to MHTML with the default conversion parameters.
C# The following example shows how to convert HTML to MHTML using C#. HTML is taken from the Web, converted to MHTML and saved to the local file system. You can download the C# SDK from the
GitHub repository .
Copy 1 // Initialize SDK API
2 var api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" ). ConvertApi ;
3
4 // Convert HTML to MHTML using ConvertUrlAsync() method
5 var result = await api. ConvertUrlAsync ( "https://example.com" , "test.mhtml" );
Java The following example shows how to convert HTML to MHTML using Java. HTML is taken from the Web, converted to MHTML and saved to the local file system. You can download the java SDK from the
GitHub repository .
Copy 1 Configuration. setBasePath ( "https://api.aspose.cloud" );
2 Configuration. setAuthPath ( "https://api.aspose.cloud/connect/token" );
3
4 HtmlApi api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" );
5
6 JobBuilder builder = new ConverterBuilder()
7 . fromUrl ( "https://example.com" )
8 . saveToLocal ( "output.mhtml" );
9
10 OperationResult result = api. convert ( builder);
C++ The following example demonstrates how to convert HTML to MHTML using C++. HTML is taken from the Web, converted to MHTML and saved to the local file system.
Copy 1 // Get current directory
2 std: : string cur_dir ( argv[ 0 ]);
3 int pos = cur_dir. find_last_of ( "/\\" );
4 cur_dir = cur_dir. substr ( 0 , pos + 1 ); // Include the last slash
5 std: : wstring w_cur_dir ( cur_dir. begin (), cur_dir. end ());
6
7 const utility:: string_t clientId = L"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ;
8 const utility:: string_t clientSecret = L"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ;
9 const utility:: string_t basePath = L"https://api.aspose.cloud/v4.0" ;
10 const utility:: string_t authPath = L"https://api.aspose.cloud/connect/token" ;
11
12 // Create configuration for authorization
13 std: : shared_ptr< ApiConfiguration> apiConfig ( new ApiConfiguration( clientId, clientSecret, basePath, authPath));
14
15 // Create client from configuration
16 std: : shared_ptr< ApiClient> apiClient ( new ApiClient( apiConfig));
17
18 // Create ConversionApi
19 std: : shared_ptr< ConversionApi> api = std:: make_shared< ConversionApi>( apiClient);
20
21 // File name for conversion
22 utility: : string_t src = L"https://example.com" ;
23 utility: : string_t dst = w_cur_dir + L"result.mhtml" ;
24
25 //Conversion
26 auto result = api-> convertUrlToLocal( src, dst);
27
28 // Check the result file
29 auto re = result-> getFile();
30 std: : ifstream f ( re. c_str ());
31 if (! f. good ())
32 {
33 throw std:: runtime_error( "Conversion failed" );
34 }
Python The following example shows how to convert HTML to MHTML Python language applying. HTML is taken from the Web, converted to MHTML and saved to the local file system. You can download the Python SDK from the
GitHub repository .
Copy 1 import os
2 from asposehtmlcloud. configuration import Configuration
3 from asposehtmlcloud. api . html_api import HtmlApi
4 from asposehtmlcloud. api_client import ApiClient as Client
5 from asposehtmlcloud. rest import ApiException
6
7 # Get keys from aspose site.
8 # There is free quota available.
9 # For more details, see https: //purchase.aspose.cloud/pricing
10
11 configuration = Configuration( apiKey= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
12 appSid= "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
13 basePath= "https://api.aspose.cloud/v4.0" ,
14 authPath= "https://api.aspose.cloud/connect/token" ,
15 debug= True)
16
17 client = Client( configuration)
18 html_api = HtmlApi( client)
19
20 try:
21 res = html_api. convert_url_to_local ( input_file= "https://example.com" , output_file= "result.mhtml" )
22 if not os. path . exists ( res. file ):
23 print( ' conversion failed' )
24 except ApiException as ex:
25 print( "Exception" )
26 print( "Info: " + str( ex))
27 raise ex
PHP The following example demonstrates how to convert HTML to MHTML using PHP. HTML is taken from the Web, converted to MHTML and saved to the local file system. You can download the PHP SDK from the
GitHub repository .
Copy 1 <? php
2 require_once ( __DIR__ . ' / vendor/ autoload. php ' );
3
4 $conf = array(
5 "basePath" => "https://api.aspose.cloud/v4.0" ,
6 "authPath" => "https://api.aspose.cloud/connect/token" ,
7 "apiKey" => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
8 "appSID" => "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
9 "defaultUserAgent" => "Webkit"
10 );
11
12 $api_html = new Client\ Invoker\ Api\ HtmlApi( $conf);
13
14 $src = ' https: //example.com';
15 $dst = ' output. mhtml ' ;
16
17 try {
18 // Request to server Api
19 $result = $api_html-> convertUrlToLocal( $src, $dst);
20 print_r( $result);
21 } catch ( Exception $e) {
22 echo ' Exception when calling HtmlApi-> convertUrlToLocal: ' , $e-> getMessage(), PHP_EOL;
23 }
24 ?>
Ruby The following example shows how to convert HTML to MHTML Ruby language applying. HTML is taken from the Web, converted to MHTML and saved to the local file system. You can download the Ruby SDK from the
GitHub repository .
Copy 1 # load the gem
2 require ' aspose_html_cloud'
3
4 # Get keys from aspose site.
5 # There is free quota available.
6 # For more details, see https: //purchase.aspose.cloud/pricing
7
8
9 CONFIG = {
10 "basePath" : "https://api.aspose.cloud/v4.0" ,
11 "authPath" : "https://api.aspose.cloud/connect/token" ,
12 "apiKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
13 "appSID" : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
14 "debug" : true
15 }
16
17 api_instance = AsposeHtml:: HtmlApi. new CONFIG
18
19 src = "https://example.com" # String | Input url.
20 dst = "test.mhtml" # String | Result file.
21
22 begin
23 # Convert the HTML file from the web and save result to the local file.
24 result = api_instance. convert_url_to_local ( src, dst)
25 p result
26 rescue AsposeHtml:: ApiError => e
27 puts "Exception when calling api_instance.convert_url_to_local: #{e}"
28 end
Node.js The following example shows how to convert HTML to MHTML Node.js language applying. HTML is taken from the Web, converted to MHTML and saved to the local file system.
Copy 1 // Get keys from aspose site.
2 // There is free quota available.
3 // For more details, see https://purchase.aspose.cloud/pricing
4
5 var conf = {
6 "basePath" : "https://api.aspose.cloud/v4.0" ,
7 "authPath" : "https://api.aspose.cloud/connect/token" ,
8 "apiKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
9 "appSID" : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
10 "defaultUserAgent" : "NodeJsWebkit"
11 };
12
13 var api = require( ' @asposecloud / aspose- html- cloud' );
14
15 // Create Conversion Api object
16 var conversionApi = new api. ConversionApi ( conf);
17
18 var src = "https://example.com" ; // {String} Url for conversion.
19 var dst = "/path/to/dst/test.mhtml" ; // {String} Result document.
20 var opts = null ;
21
22 var callback = function( error, data, response) {
23 if ( error) {
24 console. error ( error);
25 } else {
26 console. log ( data);
27 }
28 };
29
30 conversionApi. convertUrlToLocal ( src, dst, opts, callback);
Swift The following example shows how to convert HTML to MHTML Swift language applying. HTML is taken from the Web, converted to MHTML and saved to the local file system. You can download the Swift SDK from the
GitHub repository .
Copy 1 import Alamofire
2 import Foundation
3 import XCTest
4 import AsposeHtmlCloud
5
6 static let fm = FileManager. default
7 let resultDir = fm. homeDirectoryForCurrentUser . appendingPathComponent ( "Documents/Aspose.HTML.Cloud.SDK.Swift/Tests/AsposeHtmlCloudTests/TestResult" )
8
9 func fileExist ( name: String) -> Bool {
10 return FileManager. default . fileExists ( atPath: name)
11 }
12
13 ClientAPI. setConfig (
14 basePath: "https://api.aspose.cloud/v4.0" ,
15 authPath: "https://api.aspose.cloud/connect/token" ,
16 apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
17 appSID: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
18 debugging: true
19 )
20
21 let format = "mhtml"
22 let src = "https://example.com"
23 let dst = resultDir. appendingPathComponent ( "Output.\(format)" ). absoluteString
24
25 let expectation = self. expectation ( description: "testConvert to \(format)" )
26
27 HtmlAPI. convertUrlToLocal ( src: src, dst: dst, options: nil) { ( data, error) in
28
29 guard error == nil else {
30 XCTFail( "Error convert web site to \(format)). Error=\(error!.localizedDescription)" )
31 return
32 }
33 let resultPath = data!. file !
34 XCTAssertTrue( fileExist( name: resultPath))
35 expectation. fulfill ()
36 }
37 self. waitForExpectations ( timeout: 100.0 , handler: nil)
Java/Android The following example shows how to convert HTML to MHTML using Java/Android. HTML is taken from the Web, converted to MHTML and saved to the local file system. You can download the Java/Android SDK from the
GitHub repository .
Copy 1 Configuration. setBasePath ( "https://api.aspose.cloud" );
2 Configuration. setAuthPath ( "https://api.aspose.cloud/connect/token" );
3
4 HtmlApi api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" );
5
6 JobBuilder builder = new ConverterBuilder()
7 . fromUrl ( "https://example.com" )
8 . saveToLocal ( "output.mhtml" );
9
10 OperationResult result = api. convert ( builder);
CURL The following example demonstrates how to convert HTML to MHTML using REST API. HTML is taken from the Web, converted to MHTML and saved to the local file system. Follow a few required steps:
Call REST API to execute conversion (in the example below). Download conversion result back from storage using
Storage API . Copy 1 curl - X POST - v \
2 "https://api.aspose.cloud/v4.0/html/conversion/html-mhtml" \
3 - d "{'InputPath': 'https://example.com', 'OutputFile': 'test.mhtml'}" \
4 - H "Content-Type: application/json" \
5 - H "Authorization:Bearer <JWT_token>"
HTML to MHTML conversion occurs with the default conversion options : the resulting MHTML document’s width and height correspond to A4, and all margins have zero value.
Example 3. Convert HTML to MHTML inside cloud storage Aspose.HTML Cloud allows you to get an HTML file from your cloud storage and save conversion result back to cloud storage.
C# The following example shows how to convert HTML to MHTML using C#. HTML file is located in cloud storage, converted to MHTML and saved back to the cloud storage. You can download the C# SDK from the
GitHub repository .
Copy 1 // Initialize SDK API
2 var api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" ). ConvertApi ;
3
4 // Convert HTML to MHTML
5 var builder = new ConverterBuilder()
6 . FromStorageFile ( "/test.html" )
7 . ToStorageFile ( "/test.mhtml" );
8
9 var result = await api. ConvertAsync ( builder);
Java The following example shows how to convert HTML to MHTML using Java. HTML file is in the cloud storage, converted to MHTML and saved back to the cloud storage. You can download the java SDK from the
GitHub repository .
Copy 1 Configuration. setBasePath ( "https://api.aspose.cloud" );
2 Configuration. setAuthPath ( "https://api.aspose.cloud/connect/token" );
3
4 HtmlApi api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" );
5
6 JobBuilder builder = new ConverterBuilder()
7 . fromStorageFile ( "input.html" )
8 . saveToStorage ( "output.mhtml" );
9
10 OperationResult result = api. convert ( builder);
C++ The following example demonstrates how to convert HTML to MHTML using C++. HTML file is in cloud storage, converted to MHTML and saved back to the cloud storage.
Copy 1 const utility:: string_t clientId = L"XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ;
2 const utility:: string_t clientSecret = L"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ;
3 const utility:: string_t basePath = L"https://api.aspose.cloud/v4.0" ;
4 const utility:: string_t authPath = L"https://api.aspose.cloud/connect/token" ;
5
6 // Create configuration for authorization
7 std: : shared_ptr< ApiConfiguration> apiConfig ( new ApiConfiguration( clientId, clientSecret, basePath, authPath));
8
9 // Create client from configuration
10 std: : shared_ptr< ApiClient> apiClient ( new ApiClient( apiConfig));
11
12 // Create ConversionApi
13 std: : shared_ptr< ConversionApi> api = std:: make_shared< ConversionApi>( apiClient);
14
15 // File name for conversion
16 utility: : string_t src = L"file/in/storage/index.html" ;
17 utility: : string_t dst = L"result/in/storage/result.mhtml" ;
18
19 //Conversion
20 auto result = api-> convertStorageToStorage( src, dst);
Python The following example shows how to convert HTML to MHTML Python language applying. HTML file is in cloud storage, converted to MHTML and saved back to the cloud storage. You can download the Python SDK from the
GitHub repository .
Copy 1 from asposehtmlcloud. configuration import Configuration
2 from asposehtmlcloud. api . html_api import HtmlApi
3 from asposehtmlcloud. api_client import ApiClient as Client
4 from asposehtmlcloud. rest import ApiException
5
6 configuration = Configuration( apiKey= "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
7 appSid= "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
8 basePath= "https://api.aspose.cloud/v4.0" ,
9 authPath= "https://api.aspose.cloud/connect/token" ,
10 debug= True)
11 client = Client( configuration)
12 html_api = HtmlApi( client)
13
14 try:
15 res = html_api. convertApi . convert_storage_to_storage ( input_file= "test.html" , output_file= "test.mhtml" ,
16 storage_name= None)
17 except ApiException as ex:
18 print( "Exception" )
19 print( "Info: " + str( ex))
20 raise ex
PHP The following example demonstrates how to convert HTML to MHTML using PHP. HTML file is in cloud storage, converted to MHTML and saved back to the cloud storage. You can download the PHP SDK from the
GitHub repository .
Copy 1 <? php
2 require_once ( __DIR__ . ' / vendor/ autoload. php ' );
3
4 $configuration = array(
5 "basePath" => "https://api.aspose.cloud/v4.0" ,
6 "authPath" => "https://api.aspose.cloud/connect/token" ,
7 "apiKey" => "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
8 "appSID" => "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
9 "defaultUserAgent" => "Webkit"
10 );
11
12 $api_html = new HtmlApi( $configuration);
13
14 $src = "FolderInStorage/test.html" ;
15 $dst = ' FolderInStorage/ test. mhtml ' ;
16 $options = null ;
17
18 try {
19 $result = $api_html-> convertStorageToStorage( $src, $dst, null , $options);
20 print_r( $result);
21 } catch ( Exception $e) {
22 echo ' Exception when calling $api_html-> convertStorageToStorage: ' , $e-> getMessage(), PHP_EOL;
23 }
24 ?>
Ruby The following example shows how to convert HTML to MHTML Ruby language applying. HTML file is in cloud storage, converted to MHTML and saved back to the cloud storage. You can download the Ruby SDK from the
GitHub repository .
Copy 1 # load the gem
2 require ' aspose_html_cloud'
3
4 # Get keys from aspose site.
5 # There is free quota available.
6 # For more details, see https: //purchase.aspose.cloud/pricing
7
8
9 CONFIG = {
10 "basePath" : "https://api.aspose.cloud/v4.0" ,
11 "authPath" : "https://api.aspose.cloud/connect/token" ,
12 "apiKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
13 "appSID" : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
14 "debug" : true
15 }
16
17 api_instance = AsposeHtml:: HtmlApi. new CONFIG
18
19 src = "FolderInStorage/test.html" # String | Source file.
20 dst = "FolderInStorage/test.mhtml" # String | Result file.
21 storage = nil
22
23 begin
24 # Convert the file from the storage and save result to the storage.
25 result = api_instance. convert_storage_to_storage ( src, dst, storage)
26 p result
27 rescue AsposeHtml:: ApiError => e
28 puts "Exception when calling api_instance.convert_storage_to_storage: #{e}"
29 end
Node.js The following example shows how to convert HTML to MHTML Node.js language applying. HTML file is in cloud storage, converted to MHTML and saved back to the cloud storage.
Copy 1 // Get keys from aspose site.
2 // There is free quota available.
3 // For more details, see https://purchase.aspose.cloud/pricing
4
5 var conf = {
6 "basePath" : "https://api.aspose.cloud/v4.0" ,
7 "authPath" : "https://api.aspose.cloud/connect/token" ,
8 "apiKey" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
9 "appSID" : "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
10 "defaultUserAgent" : "NodeJsWebkit"
11 };
12
13 var api = require( ' @asposecloud / aspose- html- cloud' );
14
15 // Create Conversion Api object
16 var conversionApi = new api. ConversionApi ( conf);
17
18 var src = "FolderInStorage/test.html" ; // {String} Source document.
19 var dst = "FolderInStorage/test.mhtml" ; // {String} Result document.
20 var opts = null ;
21 var storage = null ;
22
23 var callback = function( error, data, response) {
24 if ( error) {
25 console. error ( error);
26 } else {
27 console. log ( data);
28 }
29 };
30
31 conversionApi. convertStorageToStorage ( src, dst, storage, opts, callback);
Swift The following example shows how to convert HTML to MHTML Swift language applying. HTML file is in cloud storage, converted to MHTML and saved back to the cloud storage. You can download the Swift SDK from the
GitHub repository .
Copy 1 import Alamofire
2 import Foundation
3 import XCTest
4 import AsposeHtmlCloud
5
6 ClientAPI. setConfig (
7 basePath: "https://api.aspose.cloud/v4.0" ,
8 authPath: "https://api.aspose.cloud/connect/token" ,
9 apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ,
10 appSID: "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" ,
11 debugging: true
12 )
13
14 let src = "FolderInStorage/test.html"
15 let dst = "FolderInStorage/test.mhtml"
16
17 let expectation = self. expectation ( description: "testConvert to mhtml" )
18
19 HtmlAPI. convertStorageToStorage ( src: src, dst: dst, storage: nil, options: nil) { ( data, error) in
20
21 guard error == nil else {
22 XCTFail( "Error convert html to mhtml. Error=\(error!.localizedDescription)" )
23 return
24 }
25
26 let resultPath = data!. file !
27
28 StorageAPI. objectExists ( path: resultPath, storageName: nil, versionId: nil) {( data, error) in
29 guard error == nil else {
30 XCTFail( "Error objectExists exist. Error=\(error!.localizedDescription)" )
31 return
32 }
33
34 XCTAssertTrue( data!. exists )
35 XCTAssertFalse( data!. isFolder )
36 expectation. fulfill ()
37 }
38 }
39 self. waitForExpectations ( timeout: 100.0 , handler: nil)
Java/Android The following example shows how to convert HTML to MHTML using Java/Android. HTML file is in cloud storage, converted to MHTML and saved back to cloud storage. You can download the Java/Android SDK from the
GitHub repository .
Copy 1 Configuration. setBasePath ( "https://api.aspose.cloud" );
2 Configuration. setAuthPath ( "https://api.aspose.cloud/connect/token" );
3
4 HtmlApi api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" );
5
6 JobBuilder builder = new ConverterBuilder()
7 . fromStorageFile ( "input.html" )
8 . saveToStorage ( "output.mhtml" );
9
10 OperationResult result = api. convert ( builder);
CURL The following example demonstrates how to convert HTML to MHTML using REST API. HTML file is located in cloud storage, converted to MHTML and saved back to cloud storage.
Copy 1 curl - X POST - v \
2 "https://api.aspose.cloud/v4.0/html/conversion/html-mhtml" \
3 - d "{'InputPath': '/test.html', 'OutputFile': '/test.mhtml'}" \
4 - H "Content-Type: application/json" \
5 - H "Authorization:Bearer <JWT_token>"
See Also The
SDK Conversion Options article describes a set of classes that represent options for converting a source HTML-based document to PDF, XPS, and image. Aspose.HTML Cloud API you can call directly from your browser by accessing the
API Reference . Aspose.HTML offers a free online
HTML to MHTML Converter that converts HTML to MHTML with high quality, ease, and speed. Just upload your files, convert them, and get the result in a few seconds!