HTML kanggo XPS

Ngonversi HTML menyang XPS

HyperText Markup Language (HTML) iku basa markup standar kanggo nggawe kaca web. Iki ngidini nggawe lan struktur bagean, paragraf, lan pranala nggunakake unsur HTML / tags. Lan yen sawetara fonts khusus digunakake ing file HTML utawa obyek dinamis sing dirujuk sing mbutuhake sambungan aktif menyang mesin sumber / server, ana kemungkinan kompromi kasetyan dokumen. Ing sisih liya, ing Spesifikasi Kertas XML(XPS), unsur kaca ditetepake kanthi bebas saka sistem operasi, printer, utawa aplikasi tampilan tartamtu. Dadi pendekatan sing cerdas yaiku ngowahi HTML menyang format XPS.

API Konversi HTML

Aspose.HTML Cloud SDK for Java minangka API basis arsitektur REST entheng sing nyedhiyakake kapabilitas kanggo nggawe, ngapusi lan ngowahi file HTML menyang PDF, DOCX, TIFF, [JPEG 6, lsp. Uga ndhukung konversi HTML menyang XPS. Dadi, pisanan, kita kudu nambah rincian ing pom.xml saka proyek jinis mbangun Maven kanggo nyakup SDK ing proyek Jawa kita.

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

Sawise instalasi, langkah sabanjure yaiku langganan gratis layanan awan liwat dasbor Aspose.Cloud nggunakake akun GitHub utawa Google. Utawa, mung gawe akun anyar lan entuk rincian Kredensial Klien sampeyan.

Ngonversi HTML menyang XPS ing Jawa

Tututi pandhuan ing ngisor iki kanggo ngembangake konverter HTML menyang XPS.

  • Kaping pisanan, kita kudu nemtokake rincian babagan metode Configuration.setAPPSID lan Configuration.setAPIKEY
  • Kapindho, kita nyetel rincian kanggo setBasePath(..), setAuthPath(..) lan nemtokake setUserAgent(…) minangka WebKit
  • Katelu, kanggo bantuan kita dhewe, kita bakal nyetel setDebug(..) minangka bener
  • Saiki gawe obyek kelas ConversionApi
  • Nemtokake rincian margin lan jeneng kanggo informasi kanggo file asil
  • Pungkasan, nelpon metode GetConvertDocumentToXps(…) sing mbutuhake jeneng HTML input, dimensi lan rincian margin minangka argumen
// kanggo conto liyane, bukak https://github.com/aspose-html-cloud/aspose-html-cloud-java

try
    {
    // Entuk ClientID lan ClientSecret saka https://dashboard.aspose.cloud/
    String clientId = "bbf94a2c-6d7e-4020-b4d2-b9809741374e";
    String clientSecret = "1c9379bb7d701c26cc87e741a29987bb";
  
    // rincian kanggo invocation 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);
        
    // Nggawe obyek saka Aspose.HTML Cloud API
    com.aspose.html.api.ConversionApi htmlApi = new ApiClient().createService(ConversionApi.class);
     	
    // Dokumen html saka panyimpenan awan
    String name = "list.html";
    // format gambar asil
    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
    	
    // Nyeluk API kanggo konversi HTML menyang 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);
    }

Kode ing ndhuwur ngasilake asil ing stream respon supaya, kanggo nyimpen output ing drive lokal, kita bisa nimbang nggunakake cara adat ing ngisor iki.

/*
* Cara njupuk ResponseBody lan jeneng file asil minangka argumen
*/
public static void checkAndSave(retrofit2.Call<okhttp3.ResponseBody> call, String fileName) throws IOException 
	{
	    // Kirim panjalukan kanthi selaras lan wangsulake tanggapane.
	    retrofit2.Response<okhttp3.ResponseBody> res = call.execute();
	    
	    // Badan respon deserialized saka respon sukses
	    okhttp3.ResponseBody answer = res.body();
	    
	    //Simpen menyang direktori test
	    boolean result = saveToDisc(answer, fileName);
	    
	    // validasi nilai asil bener (langkah opsional)
	    Assert.assertTrue(result);
	}
	
  /*
  *
  * Telpon cara iki kanggo nyimpen isi Response minangka file ing drive lokal
  *
  */ 
	public static boolean saveToDisc(okhttp3.ResponseBody body, String fileName) 
	{
            // nggawe obyek file sing nuduhake lokasi file asil
	    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;

                // nyimpen stream file menyang drive lokal
		outputStream.write(fileReader, 0, read);
		fileSizeDownloaded += read;
            }
	    // mbusak conto stream output
	    outputStream.flush();
	    
	    // bali bener minangka file disimpen kasil
	    return true;
	    } catch (Exception e) {
		e.printStackTrace();
		return false;
	    }
	} // saveToDisc ends here
HTML kanggo XPS

Gambar 1: - Pratinjau HTML nganti XPS

File sampel sing digunakake ing conto ing ndhuwur bisa diundhuh saka list.html lan resultantFile.xps.

HTML kanggo XPS nggunakake Perintah cURL

API REST uga bisa diakses liwat perintah cURL supaya ing bagean iki, kita bakal sinau langkah-langkah carane nindakake konversi HTML menyang XPS nggunakake perintah cURL. Saiki minangka prasyarat, kita kudu ngasilake JSON Web Token (JWT) adhedhasar kredensial klien individu sampeyan. Mangga nglakokake printah ing ngisor iki kanggo ngasilake token 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"

Saiki kita duwe token JWT, mangga jalanake perintah ing ngisor iki ing terminal kanggo nindakake konversi HTML menyang 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

Kesimpulan

Artikel iki wis nerangake rincian kanggo ngowahi HTML menyang XPS nggunakake REST API. Kita wis sinau langkah-langkah kanggo ngowahi HTML dadi XPS nggunakake potongan kode Java uga liwat perintah cURL. Salajengipun, elinga yen Produk Dokumentasi minangka sumber sing apik kanggo sinau babagan kemampuan luar biasa sing ditawakake API. Salajengipun, yen sampeyan nemoni masalah nalika nggunakake API, mangga hubungi Forum dhukungan produk gratis.

Artikel sing gegandhengan

Kita uga nyaranake ngunjungi blog ing ngisor iki kanggo rincian liyane babagan: