XHTML in XPS konvertieren – Cloud SDKs und REST API XHTML zu XPS Durch die Konvertierung von XHTML in XPS können Sie aus Webinhalten hochwertige, druckfertige Dokumente erstellen. XPS ist ein Dokumentformat mit festem Layout, das darauf ausgelegt ist, das Layout, die Formatierung und die grafische Wiedergabetreue von Dokumenten beizubehalten, sodass es sich zum Drucken und Teilen mit einem einheitlichen visuellen Erscheinungsbild auf allen Geräten und Plattformen eignet. Durch die Konvertierung von XHTML in XPS können Sie Dokumente in professioneller Qualität erstellen, die die Integrität des ursprünglichen Webinhalts, einschließlich Text, Bilder und Stile, wahren und gleichzeitig die Kompatibilität mit XPS-fähigen Anwendungen und Druckern gewährleisten.
Aspose.HTML Cloud v4.0 bietet die einfachste API zum Konvertieren von XHTML -Dokumenten in das XPS -Format mit hoher Qualität, einfach und schnell.
Der Artikel erklärt in einer Reihe von Codebeispielen, wie man ein XHTML-Dokument mithilfe von Aspose.HTML Cloud-Client-SDKs und REST-API-Aufrufen in XPS konvertiert. Wir werden verschiedene Szenarien der XHTML-zu-XPS-Konvertierung betrachten. SDKs sind in PHP, C++, Ruby, Python, C#, Java, Android, Swift und Node.js verfügbar.
SDK-Beispiele für die Konvertierung von XHTML in XPS Ein weit verbreiteter Anwendungsfall für Aspose.HTML Cloud SDK-Funktionen ist die Dateiverarbeitung und -konvertierung. Mit Cloud-APIs können Sie ein XHTML-Dokument anhand seines Namens von einem Speicherort oder einer lokalen Datei auf Ihrem Laufwerk abrufen, es in ein bestimmtes Format konvertieren und im Speicher oder auf einem lokalen Laufwerk speichern. Die folgenden Codebeispiele veranschaulichen, wie XHTML für verschiedene Fälle mithilfe verfügbarer SDKs und cURL in XPS konvertiert wird.
Beispiel 1. Konvertieren Sie XHTML in XPS mit Standardkonvertierungsoptionen Erwägen Sie die Konvertierung eines XHTML-Dokuments, das sich ursprünglich in einem lokalen Dateisystem befand, in das XPS-Format. Die resultierende Datei wird dann wieder im lokalen Dateisystem gespeichert. Die Konvertierung erfolgt mit Standardkonvertierungsoptionen.
C# Das folgende Beispiel zeigt die einfachste Möglichkeit, XHTML in XPS mit C# zu konvertieren. Sie können das C# SDK aus dem
GitHub-Repository herunterladen.
Copy 1 // Initialize SDK API
2 var api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" ). ConvertApi ;
3
4 // Convert XHTML to XPS
5 var result = await api. ConvertAsync ( "test.xhtml" , "test.xps" );
Java The following example demonstrates how to convert XHTML to XPS 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.xhtml" )
8 . saveToLocal ( "output.xps" );
9
10 OperationResult result = api. convert ( builder);
C++ The following example demonstrates how to convert XHTML to XPS C++ language applying. Local XHTML converted to XPS 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.xhtml" ;
23 utility: : string_t dst = w_cur_dir + L"result.xps" ;
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 XHTML to XPS 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.xhtml" , output_file= "test.xps" )
16 except ApiException as ex:
17 print( "Exception" )
18 print( "Info: " + str( ex))
19 raise ex
PHP The following example demonstrates how to convert XHTML to XPS XPS language applying. 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. xhtml ' ;
15 $dst = ' output. xps ' ;
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 XHTML to XPS 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.xhtml" # String | Full path to the input file.
19 dst = "test.xps" # 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 XHTML to XPS Node.js language applying. Local XHTML converted to XPS 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.xhtml" ; // {String} Source document.
19 var dst = "/path/to/dst/test.xps" ; // {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 XHTML to XPS 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.xhtml"
27 let format = "xps"
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 XHTML 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 XHTML to XPS 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.xhtml" )
8 . saveToLocal ( "output.xps" );
9
10 OperationResult result = api. convert ( builder);
CURL The following example demonstrates how to convert XHTML to XPS 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/XHTML-xps" \
3 - d "{'InputPath': '/testpage.xhtml', 'OutputFile': 'test.xps'}" \
4 - H "Content-Type: application/json" \
5 - H "Authorization:Bearer <JWT_token>"
Die Konvertierung von XHTML in XPS erfolgt mit den Standardkonvertierungsoptionen : Die Breite und Höhe des resultierenden XPS-Dokuments entsprechen A4 und alle Ränder haben den Wert Null.
Beispiel 2. Konvertieren Sie XHTML in XPS mit explizit angegebenen Optionen Das folgende Beispiel zeigt, wie eine XHTML-Datei mit explizit angegebenen Optionen von einem lokalen Dateisystem in XPS konvertiert und das Ergebnis wieder im lokalen Dateisystem gespeichert wird.
C# Das folgende SDK-Beispiel zeigt, wie man XHTML in XPS mit C# konvertiert. XHTML wird aus einem lokalen Dateisystem übernommen, in XPS konvertiert und auf dem lokalen Laufwerk gespeichert. Sie können das C# SDK aus dem
GitHub-Repository herunterladen.
Copy 1 // Initialize SDK API
2 var api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" ). ConvertApi ;
3
4 // Initialize conversion options
5 var options = new XPSConversionOptions()
6 . SetHeight ( 8.3 ) // A4 landscape
7 . SetWidth ( 11.7 )
8 . SetLeftMargin ( 0.2 )
9 . SetRightMargin ( 0.2 )
10 . SetBottomMargin ( 0.2 )
11 . SetTopMargin ( 0.2 );
12
13 // Convert XHTML to XPS
14 var result = await api. ConvertAsync ( "test.xhtml" , "test.xps" , options);
Java The following example demonstrates how to convert XHTML to XPS using Java. Local XHTML is converted to XPS and saved to the local drive. 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 XPSConversionOptions opt_A5 = new XPSConversionOptions()
7 . setWidth ( 5.8 ) // A5 format in inches
8 . setHeight ( 8.3 )
9 . setTopMargin ( 0.5 )
10 . setBottomMargin ( 0.5 )
11 . setLeftMargin ( 0.5 )
12 . setRightMargin ( 0.5 )
13 . setQuality ( 95 );
14
15 JobBuilder builder = new ConverterBuilder()
16 . fromLocalFile ( "input.xhtml" )
17 . useOptions ( opt_A5)
18 . saveToLocal ( "output.xps" );
19
20 OperationResult result = api. convert ( builder);
C++ The following example demonstrates how to convert XHTML to XPS using C++. Local XHTML converted to XPS and saved on your local drive.
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.xhtml" ;
23 utility: : string_t dst = w_cur_dir + L"result.xps" ;
24
25 std: : shared_ptr< ConversionOptions> opts = std:: make_shared< ConversionOptions>();
26 opts-> setWidth( 8.3 )
27 -> setHeight( 11.7 )
28 -> setLeftMargin( 0.2 )
29 -> setRightMargin( 0.2 )
30 -> setTopMargin( 0.2 )
31 -> setBottomMargin( 0.2 );
32
33 //Conversion
34 auto result = api-> convertLocalToLocal( src, dst, opts);
35
36 // Check the result file
37 auto re = result-> getFile();
38 std: : ifstream f ( re. c_str ());
39 if (! f. good ())
40 {
41 throw std:: runtime_error( "Conversion failed" );
42 }
Python The following example demonstrates how to convert XHTML to XPS Python language applying. Local XHTML converted to XPS and saved on your local drive. 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 options = {
15 ' width' : 8.3 ,
16 ' height' : 11.7 ,
17 ' topmargin' : 0.2 ,
18 ' bottommargin' : 0.2 ,
19 ' leftmargin' : 0.2 ,
20 ' rightmargin' : 0.2
21 }
22
23 try:
24 res = html_api. convertApi . convert_local_to_local ( input_file= "test.xhtml" , output_file= "test.xps" , options= options)
25 except ApiException as ex:
26 print( "Exception" )
27 print( "Info: " + str( ex))
28 raise ex
PHP The following example demonstrates how to convert XHTML to XPS using PHP. XHTML is taken from a local file system, converted to XPS and saved to the local path. 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. xhtml ' ;
15 $dst = ' output. xps ' ;
16
17 $options_a4 = [
18 ' width' => 8.3 ,
19 ' height' => 11.7 ,
20 ' left_margin' => 0.2 ,
21 ' right_margin' => 0.2 ,
22 ' top_margin' => 0.2 ,
23 ' bottom_margin' => 0.2
24 ];
25
26
27 try {
28 // Request to server Api
29 $result = $api_html-> convertLocalToLocal( $src, $dst, $options_a4);
30 print_r( $result);
31 } catch ( Exception $e) {
32 echo ' Exception when calling HtmlApi-> convertLocalToLocal: ' , $e-> getMessage(), PHP_EOL;
33 }
34 ?>
Ruby The following example demonstrates how to convert XHTML to XPS Ruby language applying. Local XHTML converted to XPS and saved on your local drive. 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 = "test.xhtml" # String | Source file.
20 dst = "test.xps" # String | Result file.
21
22 opts = {
23 width: 8.3 , # Float | Resulting document width in inches.
24 height: 11.7 , # Float | Resulting document height in inches.
25 left_margin: 0.2 , # Float | Left resulting document margin in inches.
26 right_margin: 0.2 , # Float | Right resulting document margin in inches.
27 top_margin: 0.2 , # Float | Top resulting document margin in inches.
28 bottom_margin: 0.2 # Float | Bottom resulting document margin in inches.
29 }
30
31 begin
32 # Convert the HTML file from the local file and save result to the local file.
33 result = api_instance. convert_local_to_local ( src, dst, opts)
34 p result
35 rescue AsposeHtml:: ApiError => e
36 puts "Exception when calling api_instance.convert_local_to_local: #{e}"
37 end
Node.js The following example demonstrates how to convert XHTML to XPS Node.js language applying. Local XHTML converted to XPS and saved on your local drive.
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.xhtml" ; // {String} Source document.
19 var dst = "/path/to/dst/test.xps" ; // {String} Result document.
20 var opts = {
21 ' width' : 8.3 ,
22 ' height' : 11.7 ,
23 ' leftMargin' : 0.2 ,
24 ' rightMargin' : 0.2 ,
25 ' topMargin' : 0.2 ,
26 ' bottomMargin' : 0.2
27 };
28
29 var callback = function( error, data, response) {
30 if ( error) {
31 console. error ( error);
32 } else {
33 console. log ( data);
34 }
35 };
36
37 conversionApi. convertLocalToLocal ( src, dst, opts, callback);
Swift The following example demonstrates how to convert XHTML to XPS Swift language applying. Local XHTML converted to XPS and saved on your local drive. 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.xhtml"
27 let format = "xps"
28 let src = url( forResource: fileName). absoluteString
29 let options = Options( width: 8.3 , height: 11.7 , leftMargin: 0.2 ,
30 rightMargin: 0.2 , topMargin: 0.2 , bottomMargin: 0.2 )
31
32
33 let expectation = self. expectation ( description: "testConvert to \(format)" )
34 let dst = resultDir. appendingPathComponent ( "Output.\(format)" ). absoluteString
35 HtmlAPI. convertLocalToLocal ( src: src, dst: dst, options: options) { ( data, error) in
36
37 guard error == nil else {
38 XCTFail( "Error convert XHTML to \(format)). Error=\(error!.localizedDescription)" )
39 return
40 }
41 let resultPath = data!. file !
42 XCTAssertTrue( fileExist( name: resultPath))
43 expectation. fulfill ()
44 }
45 self. waitForExpectations ( timeout: 100.0 , handler: nil)
Java/Android The following example demonstrates how to convert XHTML to XPS using Java/Android. Local XHTML converted to XPS and saved on your local drive. 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 XPSConversionOptions opt_A5 = new XPSConversionOptions()
7 . setWidth ( 5.8 ) // A5 format in inches
8 . setHeight ( 8.3 )
9 . setTopMargin ( 0.5 )
10 . setBottomMargin ( 0.5 )
11 . setLeftMargin ( 0.5 )
12 . setRightMargin ( 0.5 )
13 . setQuality ( 95 );
14
15 JobBuilder builder = new ConverterBuilder()
16 . fromLocalFile ( "input.xhtml" )
17 . useOptions ( opt_A5)
18 . saveToLocal ( "output.xps" );
19
20 OperationResult result = api. convert ( builder);
CURL The following example demonstrates how to convert XHTML to XPS using REST API. Local XHTML converted to XPS and saved to the local file system. Follow a few required steps:
Upload a 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/XHTML-xps" \
3 - d "{'InputPath': '/testpage.xhtml', 'OutputFile': 'test.xps', 'Options': {'Width':1000, 'Height': 800, 'LeftMargin': 10, 'RightMargin': 10, 'TopMargin': 10, 'BottomMargin': 10}}" \
4 - H "Content-Type: application/json" \
5 - H "Authorization: bearer <token>"
Weitere Einzelheiten zu den verfügbaren Konvertierungsparametern für XHTML-Dateien finden Sie im Abschnitt
SDK-Konvertierungsoptionen .
Beispiel 3. Konvertieren Sie XHTML in XPS im Cloud-Speicher Mit Aspose.HTML Cloud können Sie eine XHTML-Datei aus Ihrem Cloud-Speicher abrufen und das Konvertierungsergebnis wieder im Cloud-Speicher speichern. Schauen wir uns ein Beispiel einer Konvertierung mit Standardspeicheroptionen an.
C# Das folgende Beispiel zeigt, wie man XHTML in XPS mit C# konvertiert. Die XHTML-Datei befindet sich im Cloud-Speicher, wird in XPS konvertiert und wieder im Cloud-Speicher gespeichert. Sie können das C# SDK aus dem
GitHub-Repository herunterladen.
Copy 1 // Initialize SDK API
2 var api = new HtmlApi( "CLIENT_ID" , "CLIENT_SECRET" ). ConvertApi ;
3
4 // Convert XHTML to XPS
5 var builder = new ConverterBuilder()
6 . FromStorageFile ( "/test.xhtml" )
7 . ToStorageFile ( "/test.xps" );
8
9 var result = await api. ConvertAsync ( builder);
Java The following example shows how to convert XHTML to XPS using Java. XHTML file is located in cloud storage, converted to XPS 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.xhtml" )
8 . saveToStorage ( "output.xps" );
9
10 OperationResult result = api. convert ( builder);
C++ The following example demonstrates how to convert XHTML to XPS using C++. XHTML file is located in cloud storage, converted to XPS 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.xhtml" ;
17 utility: : string_t dst = L"result/in/storage/result.xps" ;
18
19 //Conversion
20 auto result = api-> convertStorageToStorage( src, dst);
Python The following example shows how to convert XHTML to XPS Python language applying. XHTML file is located in cloud storage, converted to XPS 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.xhtml" , output_file= "test.xps" ,
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 XHTML to XPS using PHP. XHTML file is located in cloud storage, converted to XPS 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.xhtml" ;
15 $dst = ' FolderInStorage/ test. xps ' ;
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 XHTML to XPS Ruby language applying. XHTML file is located in cloud storage, converted to XPS and saved back to 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.xhtml" # String | Source file.
20 dst = "FolderInStorage/test.xps" # 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 XHTML to XPS Node.js language applying. XHTML file is located in cloud storage, converted to XPS and saved back to 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.xhtml" ; // {String} Source document.
19 var dst = "FolderInStorage/test.xps" ; // {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 XHTML to XPS Swift language applying. XHTML file is located in cloud storage, converted to XPS and saved back to 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.xhtml"
15 let dst = "FolderInStorage/test.xps"
16
17 let expectation = self. expectation ( description: "testConvert to xps" )
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 XHTML to xps. 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 XHTML to XPS using Java/Android. XHTML file is located in cloud storage, converted to XPS 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.xhtml" )
8 . saveToStorage ( "output.xps" );
9
10 OperationResult result = api. convert ( builder);
CURL The following example demonstrates how to convert XHTML to XPS using REST API. XHTML file is located in cloud storage, converted to XPS and saved back to cloud storage.
Copy 1 curl - X POST - v \
2 "https://api.aspose.cloud/v4.0/html/conversion/XHTML-xps" \
3 - d "{'InputPath': '/test.xhtml', 'OutputFile': '/test.xps'}" \
4 - H "Content-Type: application/json" \
5 - H "Authorization:Bearer <JWT_token>"
Siehe auch Der Artikel
Verfügbare SDKs stellt Ihnen die Fähigkeit von Aspose.HTML Cloud vor, SDKs in verschiedenen Programmiersprachen wie C#, Java, Python, Ruby, PHP, Node.js, Swift, Android und C++ zu verwenden. Der Artikel
SDK-Konvertierungsoptionen beschreibt eine Reihe von Klassen, die Optionen zum Konvertieren eines HTML-basierten Quelldokuments in PDF, XPS und Bild darstellen. Aspose.HTML Cloud API können Sie direkt von Ihrem Browser aus aufrufen, indem Sie auf die
API-Referenz zugreifen. Aspose.HTML bietet einen kostenlosen Online-
XHTML-zu-XPS-Konverter , der XHTML schnell und in hoher Qualität in XPS konvertiert. Laden Sie einfach Ihre Dateien hoch, konvertieren Sie sie und erhalten Sie das Ergebnis in wenigen Sekunden!