HTML shine tsarin defacto don tsara shafukan yanar gizo kuma yana adana abun ciki a daidaitaccen tsarin rubutu. Alamun da ke cikin HTML suna bayyana shimfidar shafi da abun ciki na shafin yanar gizon, gami da rubutu, teburi, hotuna, da manyan hanyoyin haɗin yanar gizo, waɗanda ake nunawa a cikin burauzar gidan yanar gizon. Koyaya, a ƙarshe, an lura cewa ana iya shigar da rubutun qeta a cikin shafukan HTML kuma ana iya amfani da su a cikin nau’ikan hari iri-iri, gami da rubutun giciye (XSS). Saboda haka, ƙungiyoyi/tsari da yawa suna toshe loda fayilolin HTML da aka raba a yanayin layi. Don haka mafita mai iya aiki shine canza HTML zuwa Tsarin Hoto. A cikin wannan labarin, za mu tattauna cikakken bayani game da yadda ake canza HTML zuwa JPG a cikin Java.
- HTML zuwa API Canjin Hoto
- Yadda ake canza HTML zuwa JPG a Java
- Maida HTML zuwa JPG ta amfani da Umarnin CURL
HTML zuwa API Canjin Hoto
Za mu yi amfani da Aspose.HTML Cloud SDK don Java don yin HTML zuwa canza Hoto. Wannan API ɗin yana ba da fasalin don lodawa da sarrafa fayilolin HTML masu wanzuwa. A lokaci guda, yana kuma ba da fasalin don sanya HTML zuwa PDF, XPS, DOCX, da tsarin hoto gami da (JPEG, PNG, BMP, da TIFF). Yanzu da fatan za a ƙara layin masu zuwa zuwa pom.xml na nau’in ginin maven ɗin ku don saukewa da shigar da SDK.
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html-cloud</artifactId>
<version>20.7.0</version>
</dependency>
Babban mataki na gaba shine biyan kuɗi kyauta ga ayyukan girgijenmu ta 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.
Yadda ake canza HTML zuwa JPG a Java
Da fatan za a bi umarnin da aka bayar a ƙasa don cika buƙatun canza HTML zuwa JPG.
- 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 GetConvertDocumentToImage(…) don fara aiwatar da juyawa. Wannan hanyar tana karɓar sunan HTML na shigarwa, tsarin hoto na sakamako, gefe, da cikakkun bayanai masu girma kamar gardama
// 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 JPG
retrofit2.Call<okhttp3.ResponseBody> call = htmlApi.GetConvertDocumentToImage(name, outFormat, width, height, leftMargin, rightMargin, topMargin, bottomMargin, resolution, folder, storage);
// (Hanyar al'ada na zaɓi don adana sakamakon JPG zuwa drive ɗin gida)
checkAndSave(call, "resultantFile.png");
System.out.println("HTML to JPG conversion sucessfull !");
}catch(Exception ex)
{
System.out.println(ex);
}
Maida HTML zuwa JPG ta amfani da Umarnin CURL
Aspose.HTML Cloud APIs kuma ana iya samun dama ga umarnin cURL ta amfani da tashoshi layin umarni. Amma a matsayin buƙatu na 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"
Da zarar an ƙirƙiri alamar JWT, da fatan za a aiwatar da umarni mai zuwa akan tashar don yin canjin HTML zuwa Hoto.
curl -v -X GET "https://api.aspose.cloud/html/list.html/convert/image/JPG" \
-H "accept: multipart/form-data" \
-H "authorization: Bearer <JWT Token>"
Kammalawa
Mun koyi juyar da HTML zuwa Hoto ta amfani da snippets code na Java da kuma cikakkun bayanai kan yadda zamu iya canza HTML zuwa JPG ta amfani da umarnin cURL. Samfurin Takardu babban tushe ne don koyan wasu iyakoki masu ban mamaki da API ke bayarwa. Hakanan, idan kun ci karo da wasu batutuwa yayin amfani da API, da fatan za ku ji daɗin tuntuɓar Zauren tallafin samfur kyauta.
Labarai masu alaka
Muna kuma ba da shawarar ziyartar shafukan yanar gizo masu zuwa don ƙarin cikakkun bayanai game da: