JPG zuwa PDF

Koyi yadda ake canza JPG zuwa PDF a Python

Hotunan JPG ko JPEG suna daga cikin shahararrun hotunan raster yayin da suke amfani da hadadden matsi na algorithm wanda ke baiwa masu amfani damar ƙirƙirar ƙananan zane. Yawancin na’urori da suka haɗa da tebur, wayar hannu, da sauran na’urorin hannu suna tallafawa hotunan JPG. Yanzu idan muna buƙatar raba manyan hotuna, to, jujjuya JPG zuwa [PDF] da alama shine mafita mai yuwuwa. A cikin wannan labarin, za mu tattauna cikakkun bayanai kan yadda ake canza JPG zuwa PDF a Python.

Kwanan nan mun haɓaka sabis na Rubutu zuwa GIF kyauta, wanda zaku iya amfani da shi don ƙirƙirar raye-raye masu ban sha’awa daga rubutu masu sauƙi.

API ɗin Canjin JPG zuwa PDF

Iyalin wannan labarin ya keɓe ga yaren Python, don haka za mu jaddada amfani da Aspose.PDF Cloud SDK for Python. Yana da abin rufewa a kusa da Cloud REST API kuma yana ba ku damar aiwatar da duk damar sarrafa fayilolin PDF a cikin aikace-aikacen Python. Yana ba da damar ƙirƙira, shiryawa da canza fayilolin PDF zuwa nau’ikan tsararrun tallafi masu tallafi. Hakanan kuna iya loda fayiloli iri-iri ciki har da EPUB, PS, SVG, XPS, JPEG, da sauransu, sannan ku canza su zuwa PDF.

Yanzu mataki na farko shine shigar da SDK wanda ke akwai don saukewa akan PIP da GitHub ma’aji. Da fatan za a aiwatar da umarni mai zuwa akan tasha / umarni da sauri don shigar da sabuwar sigar SDK akan tsarin.

 pip install asposepdfcloud

Bayan shigarwa, babban mataki na gaba shine biyan kuɗi kyauta ga ayyukan girgijenmu ta Aspose.Cloud dashboard. Manufar wannan biyan kuɗi shine don ba wa izini izini kawai don samun damar ayyukan sarrafa fayil ɗin mu. Idan kuna da asusun GitHub ko Google, kawai Yi rajista ko, danna maɓallin Ƙirƙiri sabon Asusu.

Canza JPG zuwa PDF a cikin Python

Juyawa ta amfani da snippet code na Python yana da amfani sosai. Da fatan za a bi matakan da aka ayyana a ƙasa don cika wannan buƙatu.

  • Da farko, ƙirƙiri misalin aji na ApiClient yayin ba da ID na Abokin ciniki & Sirrin Abokin ciniki azaman muhawara
  • Na biyu, ƙirƙiri wani abu na ajin PdfApi wanda ke ɗaukar abu ApiClient azaman hujjar shigarwa
  • Na uku, ƙirƙirar misali na ImageTemplatesRequest class inda muke ayyana sunan hoton tushe, da kaddarorin da ke da alaƙa kamar faɗin, tsayi, cikakkun bayanan gefe.
  • A ƙarshe, kira hanyar putimageinstoragetopdf(..) na ajin PdfApi don aiwatar da juyawa.
# sami takardun shaidar abokin ciniki daga https://dashboard.aspose.cloud/

def image2PDF():
    try:
        #Client credentials
        client_secret = "1c9379bb7d701c26cc87e741a29987bb"
        client_id = "bbf94a2c-6d7e-4020-b4d2-b9809741374e"

        #initialize PdfApi client instance using client credetials
        pdf_api_client = asposepdfcloud.api_client.ApiClient(client_secret, client_id)

        # ƙirƙirar misalin PdfApi yayin wucewa PdfApiClient azaman hujja
        pdf_api = PdfApi(pdf_api_client)

        #source image file
        input_file = 'source.jpg'

        #resultant PDF document
        resultant_file = 'Resultant.pdf'

        image_templates_details = asposepdfcloud.ImageTemplatesRequest
        {
            "IsOCR": True,
            "OCRLangs": "eng",
            "ImagesList": [
            {
                "ImagePath": input_file,
                "ImageSrcType": "ImageSrcType.Common",
                "LeftMargin": 10,
                "RightMargin": 10,
                "TopMargin": 10,
                "BottomMargin": 10,
                "PageWidth": 800,
                "PageHeight": 1000,
                "MarginInfo": {
                    "Left": 10,
                    "Right": 10,
                    "Top": 10,
                    "Bottom": 10
                  }
            }
            ]
        }
        
        # kira API don canza hoto zuwa tsarin PDF
        response = pdf_api.put_image_in_storage_to_pdf(name=resultant_file, image_templates= image_templates_details)

        # buga sakon a cikin na'ura mai kwakwalwa (na zaɓi)
        print('Image successfully converted to PDF format !')    
    except ApiException as e:
        print("Exception while calling PdfApi: {0}".format(e))
        print("Code:" + str(e.code))
        print("Message:" + e.message)

Idan muna buƙatar musanya hoto fiye da ɗaya a cikin kira ɗaya, da fatan za a saka ƙarin misalan Hotunan Hotuna a cikin Hotunan Abubuwan Buƙatar Buƙatar.

Canza JPG zuwa PDF preview

Hoto 1: - JPG zuwa PDF samfoti.

JPG zuwa PDF ta amfani da Umarnin CURL

Dokokin CURL suna ba da hanya mai dacewa don samun damar APIs REST akan tashar layin umarni. Tun da Aspose.PDF Cloud an haɓaka shi kamar yadda tsarin gine-gine na REST, don haka za mu iya samun dama gare shi ta umarnin cURL. Yanzu mataki na farko shine ƙirƙirar Token Yanar Gizo na JSON (JWT) dangane da takaddun shaidar abokin cinikin ku. Don haka 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 yi amfani da umarnin CURL mai zuwa don canza tsarin JPG zuwa PDF. Ana loda fayil ɗin sakamakon zuwa ma’ajiyar gajimare.

curl -v -X PUT "https://api.aspose.cloud/v3.0/pdf/Resultant.pdf/create/images" \
-H  "accept: application/json" \
-H  "authorization: Bearer <JWT Token>" \
-H  "Content-Type: application/json" \
-d "{  \"IsOCR\": true,  \"OCRLangs\": \"eng\",  \"ImagesList\": [    {      \"ImagePath\": \"source.jpg\",      \"ImageSrcType\": \"Common\",      \"LeftMargin\": 10,      \"RightMargin\": 10,      \"TopMargin\": 10,      \"BottomMargin\": 10,      \"PageWidth\": 800,      \"PageHeight\": 1000,      \"MarginInfo\": {        \"Left\": 10,        \"Right\": 10,        \"Top\": 10,        \"Bottom\": 10      }    }  ]}"

Don bayanin ku, tushen fayil ɗin JPG da sakamakon daftarin aiki na PDF ana loda shi akan source.jpg da Image2PDF-Resultant.pdf.

Kammalawa

A cikin wannan labarin, mun tattauna matakan canza JPG zuwa PDF ta amfani da Python. Yayin bin umarnin guda ɗaya, za mu iya kuma canza hotuna PNG, BMP, da GIF zuwa PDF. Mun kuma koya game da tsarin canza JPG zuwa PDF ta amfani da umarnin CURL. Koyaya, SDKs ɗin mu na Cloud an haɓaka su ƙarƙashin lasisin MIT, don haka ana samun cikakkiyar snippet ɗin su don saukewa kyauta akan GitHub.

Idan kuna da wasu tambayoyi masu alaƙa ko kun ci karo da kowace matsala yayin amfani da APIs ɗin mu, da fatan za ku ji daɗin tuntuɓar mu ta [ dandalin tallafin abokin ciniki kyauta 15.

Labarai masu alaka

Muna kuma ba da shawarar ziyartar hanyoyin haɗin yanar gizon don ƙarin koyo game da su