HTML zuwa XPS

Tukar HTML ke XPS

Harshen Markup na HyperText (HTML) daidaitaccen harshe ne don ƙirƙirar shafin yanar gizon. Yana ba da damar ƙirƙira da tsarin sassa, sakin layi, da hanyoyin haɗin gwiwa ta amfani da abubuwan HTML/tags. Kuma idan an yi amfani da wasu nau’ikan rubutu na al’ada a cikin fayil ɗin HTML ko kowane abu mai ƙarfi yana yin nuni wanda ke buƙatar haɗin kai mai aiki zuwa na’ura/uwar garke, akwai yuwuwar lalata amincin takaddar. A gefe guda, a cikin Takaddun Takaddun XML (XPS), an ayyana abubuwan shafin ba tare da wani tsarin aiki ba, firinta, ko aikace-aikacen kallo. Don haka hanya mai hankali ita ce canza HTML zuwa tsarin XPS.

API ɗin Canjin HTML

Aspose.HTML Cloud SDK don Java shine API ɗin gini mai sauƙi na REST yana ba da damar ƙirƙira, sarrafa da canza fayilolin HTML zuwa PDF, DOCX, TIFF, [JPEG. 6, da sauransu. Hakanan yana goyan bayan canza HTML zuwa XPS. Don haka, da farko, muna buƙatar ƙara waɗannan cikakkun bayanai a cikin pom.xml na nau’in ginin Maven don haɗa SDK a cikin aikin Java ɗin mu.

<repositories> 
    <repository>
        <id>aspose-cloud</id>
        <name>artifact.aspose-cloud-releases</name>
        <url>http://artifact.aspose.cloud/repo</url>
    </repository>   
</repositories>

<dependencies>
    <dependency>
        <groupId>com.aspose</groupId>
	<artifactId>aspose-html-cloud</artifactId>
	<version>20.7.0</version>
	<scope>compile</scope>
    </dependency>
</dependencies>

Bayan shigarwa, mataki na gaba shine biyan kuɗi kyauta ga ayyukan girgije ta hanyar Aspose.Cloud dashboard ta amfani da GitHub ko asusun Google. Ko, a sauƙaƙe ƙirƙiri sabon Asusu kuma sami cikakkun bayanan Shaidar Abokin Ciniki.

Maida HTML zuwa XPS a Java

Da fatan za a bi umarnin da aka kayyade a ƙasa don haɓaka HTML zuwa mai sauya XPS.

  • Da farko, muna buƙatar ƙayyade cikakkun bayanai game da hanyoyin Configuration.setAPPSID da Configuration.setAPIKEY
  • Abu na biyu, mun saita cikakkun bayanai don setBasePath(..), setAuthPath(..) da saka setUserAgent(…) azaman WebKit
  • Na uku, don taimakon kanmu, za mu saita saitinDebug(..) a matsayin gaskiya
  • Yanzu ƙirƙiri wani abu na ajin ConversionApi
  • Ƙayyade bayanan gefe da suna don bayani don fayil ɗin sakamako
  • A ƙarshe, kira GetConvertDocumentToXps(…) wanda ke buƙatar shigar da sunan HTML, girma da cikakkun bayanan gefe azaman muhawara.
// don ƙarin misalai, da fatan za a ziyarci https://github.com/aspose-html-cloud/aspose-html-cloud-java

try
    {
    // Samu ClientID da ClientSecret daga https://dashboard.aspose.cloud/
    String clientId = "bbf94a2c-6d7e-4020-b4d2-b9809741374e";
    String clientSecret = "1c9379bb7d701c26cc87e741a29987bb";
  
    // cikakkun bayanai don kiran API
    com.aspose.html.Configuration.setAPP_SID(clientId);
    com.aspose.html.Configuration.setAPI_KEY(clientSecret);
    com.aspose.html.Configuration.setBasePath("https://api.aspose.cloud/v3.0");
    com.aspose.html.Configuration.setAuthPath("https://api.aspose.cloud/connect/token");
    com.aspose.html.Configuration.setUserAgent("WebKit");
    com.aspose.html.Configuration.setDebug(true);
        
    // Ƙirƙiri wani abu na Aspose.HTML Cloud API
    com.aspose.html.api.ConversionApi htmlApi = new ApiClient().createService(ConversionApi.class);
     	
    // Takardun html daga ma'ajiyar girgije
    String name = "list.html";
    // tsarin hoto na sakamako
    String outFormat = "PNG";
    	
    Integer width = 800; // Resulting image width.
    Integer height = 1000; // Resulting image height.
    Integer leftMargin = 10; // Left resulting image margin.
    Integer rightMargin = 10; // Right resulting image margin.
    Integer topMargin = 10; // Top resulting image margin.
    Integer bottomMargin = 10; // Bottom resulting image margin.
    Integer resolution = 300; // Resolution of resulting image.
    String folder = null; // The folder in the storage. Should exist.
    String storage = "Internal"; // Name of the storage. null
    	
    // Kira API don canza HTML zuwa XPS
    retrofit2.Call<okhttp3.ResponseBody> call = htmlApi.GetConvertDocumentToXps(name, width, height, leftMargin, rightMargin, topMargin, bottomMargin, folder, storage);
      
    System.out.println("HTML to XPS conversion sucessfull !");
    }catch(Exception ex)
    {
        System.out.println(ex);
    }

Lambar da ke sama tana mayar da sakamakon a cikin rafin amsawa don haka, domin adana abin fitarwa a kan tuƙi na gida, ƙila mu yi la’akari da yin amfani da hanyar al’ada mai zuwa.

/*
* Hanyar ɗaukar ResponseBody da sunan fayil mai sakamako azaman gardama
*/
public static void checkAndSave(retrofit2.Call<okhttp3.ResponseBody> call, String fileName) throws IOException 
	{
	    // Yi aiki tare da aika buƙatar kuma mayar da martani.
	    retrofit2.Response<okhttp3.ResponseBody> res = call.execute();
	    
	    // Jikin amsawa mara kyau na amsa mai nasara
	    okhttp3.ResponseBody answer = res.body();
	    
	    //Ajiye don gwada kundin adireshi
	    boolean result = saveToDisc(answer, fileName);
	    
	    // tabbatar da ƙimar sakamakon gaskiya ne (mataki na zaɓi)
	    Assert.assertTrue(result);
	}
	
  /*
  *
  * Kira wannan hanyar don adana abun ciki na Amsa azaman fayil akan faifan gida
  *
  */ 
	public static boolean saveToDisc(okhttp3.ResponseBody body, String fileName) 
	{
            // ƙirƙiri abun fayil mai nuna wuri don fayil ɗin sakamako
	    File savedFile = new File("c:\\Downloads\\"+fileName);
      
	    try (InputStream inputStream = body.byteStream();
	    OutputStream outputStream = new FileOutputStream(savedFile))
	    {
	    byte[] fileReader = new byte[4096];
	    long fileSizeDownloaded = 0;

	    while (true) {
	        int read = inputStream.read(fileReader);
		if (read == -1) break;

                // ajiye rafin fayil zuwa faifan gida
		outputStream.write(fileReader, 0, read);
		fileSizeDownloaded += read;
            }
	    // share misalin rafin fitarwa
	    outputStream.flush();
	    
	    // dawo da gaskiya a matsayin adana fayil cikin nasara
	    return true;
	    } catch (Exception e) {
		e.printStackTrace();
		return false;
	    }
	} // saveToDisc ends here
HTML zuwa XPS

Hoto 1: - samfotin HTML zuwa XPS

Za a iya sauke samfurin fayilolin da aka yi amfani da su a cikin misalin da ke sama daga list.html da resultantFile.xps.

HTML zuwa XPS ta amfani da Umarnin CURL

Hakanan ana iya samun dama ga REST APIs ta umarnin cURL don haka a cikin wannan sashe, za mu koyi matakai kan yadda ake yin canjin HTML zuwa XPS ta amfani da umarnin cURL. Yanzu a matsayin buƙatun farko, muna buƙatar fara samar da Token Yanar Gizo na JSON (JWT) dangane da takaddun shaidar abokin cinikin ku. Da fatan za a aiwatar da umarni mai zuwa don samar da alamar JWT.

curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=bbf94a2c-6d7e-4020-b4d2-b9809741374e&client_secret=1c9379bb7d701c26cc87e741a29987bb" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"

Yanzu da muke da alamar JWT, da fatan za a aiwatar da umarni mai zuwa akan tashar don yin canjin HTML zuwa XPS.

curl -v -X GET "https://api.aspose.cloud/html/list.html/convert/xps" \
-H  "accept: multipart/form-data" \
-H  "authorization: Bearer <JWT Token>" \
-o final.xps

Kammalawa

Wannan labarin ya bayyana cikakkun bayanai don canza HTML zuwa XPS ta amfani da REST API. Mun koyi matakan canza HTML zuwa XPS ta amfani da snippets code na Java da kuma ta hanyar cURL umarni. Bugu da ƙari, da fatan za a lura cewa Samfurin Takardu babban tushe ne don koyan iyakoki masu ban mamaki da API ke bayarwa. Bugu da ƙari, idan kun ci karo da wasu batutuwa yayin amfani da API, da fatan za ku ji daɗi don tuntuɓar Zauren tallafin samfur na Kyauta.

Labarai masu alaka

Muna kuma ba da shawarar ziyartar shafukan yanar gizo masu zuwa don ƙarin cikakkun bayanai game da: