HTML menyang Gambar

Carane ngowahi HTML menyang Gambar ing Jawa

HTML minangka format defacto kanggo nyusun kaca web lan nyimpen isi ing format teks standar. Tag ing HTML nemtokake tata letak kaca lan isi kaca web, kalebu teks, tabel, gambar, lan hyperlink, sing ditampilake ing browser web. Nanging, pungkasan, diamati manawa skrip jahat bisa diselehake ing kaca HTML lan bisa digunakake ing macem-macem jinis serangan, kalebu skrip lintas situs (XSS). Mulane, akeh organisasi / sistem ngalangi loading file HTML sing dienggo bareng ing mode offline. Dadi solusi sing bisa ditindakake yaiku ngowahi HTML menyang format Gambar. Ing artikel iki, kita bakal ngrembug rincian babagan carane ngowahi HTML dadi JPG ing Jawa.

HTML menyang API Konversi Gambar

Kita bakal nggunakake Aspose.HTML Cloud SDK for Java kanggo nindakake HTML menyang konversi Gambar. API iki nyedhiyakake fitur kanggo mbukak lan ngapusi file HTML sing ana. Ing wektu sing padha, uga nawakake fitur kanggo nerjemahake HTML menyang PDF, XPS, DOCX, lan format gambar kalebu (JPEG, PNG, BMP, lan TIFF). Saiki tambahake baris ing ngisor iki menyang pom.xml proyek jinis mbangun maven kanggo ngundhuh lan nginstal SDK.

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

Langkah utama sabanjure yaiku langganan gratis kanggo layanan awan liwat Aspose.Cloud dashboard nggunakake akun GitHub utawa Google. Utawa, mung gawe akun anyar lan entuk rincian Kredensial Klien sampeyan.

Carane ngowahi HTML dadi JPG ing Jawa

Tututi pandhuan ing ngisor iki kanggo ngrampungake syarat ngowahi HTML dadi JPG.

  • 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 GetConvertDocumentToImage(…) kanggo miwiti proses konversi. Cara iki nampa jeneng HTML input, format gambar asil, wates, lan rincian dimensi 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
    	
    // Njaluk API kanggo konversi HTMl menyang JPG
    retrofit2.Call<okhttp3.ResponseBody> call = htmlApi.GetConvertDocumentToImage(name, outFormat, width, height, leftMargin, rightMargin, topMargin, bottomMargin, resolution, folder, storage);
    
    // (cara khusus opsional kanggo nyimpen JPG asil menyang drive lokal)
    checkAndSave(call, "resultantFile.png");
  
    System.out.println("HTML to JPG conversion sucessfull !");
    }catch(Exception ex)
    {
        System.out.println(ex);
    }

Ngonversi HTML menyang JPG nggunakake Perintah cURL

Aspose.HTML Cloud API uga bisa diakses liwat printah cURL nggunakake terminal baris printah. Nanging 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"

Sawise token JWT wis digawe, mangga nglakokake printah ing ngisor iki ing terminal kanggo nindakake HTML kanggo konversi Gambar.

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

Kesimpulan

Kita wis sinau konversi HTML menyang Gambar nggunakake potongan kode Java uga rincian babagan carane kita bisa ngowahi HTML dadi JPG nggunakake perintah cURL. Produk Dokumentasi minangka sumber sing apik kanggo sinau kemampuan luar biasa liyane sing ditawakake API. Uga, yen sampeyan nemoni masalah nalika nggunakake API, hubungi Forum dhukungan produk gratis.

Artikel sing gegandhengan

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