PHP: DOCX to PDF Conversion with Aspose.Words Cloud Issue

As the title suggests when i use the online demo (Convert Word, PDF And Other File Formats In PHP) works like a charm. As soon as I try to replicate the code in my project I get an incorrect conversion.

I have tried to use both the API and the Composer package but none of the two work. In the examples below I removed the secret and client ID for security purposes but I do use the actual values in my tests

1: API

  $client = new Client(
            [
                'headers' => [
                    'Accept' => 'application/json',
                    'Content-Type' => 'application/x-www-form-urlencoded'
                ]
            ]
        );
        $res = $client->post('https://api.aspose.cloud/connect/token', [
            'form_params' => [
                'grant_type' => 'client_credentials',
                'client_id' => 'my client id here',           
                'client_secret' => 'my secret here',
            ]
        ]);
        
        if (!$res->getStatusCode() == 200) {
            return 'Could not get bearer token';
        }

        $response = json_decode($res->getBody()->getContents(), true);
        $accessToken = $response['access_token'];

        $curl = curl_init();
        
        curl_setopt_array($curl, array(
            CURLOPT_URL => 'https://api.aspose.cloud/v4.0/words/convert?format=pdf',
            CURLOPT_CUSTOMREQUEST => 'PUT',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => array(
                'document' => new CURLFile(storage_path('Default Template.docx')),
            ),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer ' .  $accessToken,
                'Content-Type: multipart/form-data'
            ),
        ));
        
        $convertedFile = curl_exec($curl);
        curl_close($curl);

2: PHP Code

        $wordsApi = new WordsApi('my client id', 'my secret here');
        $requestDocument = storage_path('Default Template.docx');
        $request = new ConvertDocumentRequest($requestDocument, "pdf", NULL, NULL, NULL, NULL);
        $file = $wordsApi->convertDocument($request);

Which gives me a result of

%PDF-1.7 %ÈÉÊË 5 0 obj <>/XObject<>>>/Group<>>> endobj 6 0 obj <>stream xœÍXMsÛ6ý+8Úa!ˆÏ£c%™äÐ6ŽÒvÆ“ƒkÓ±f$9µäxúï»äÝ8–¥Ê32ôû¸Ø· $ð7‘ðåµäA¡´–ìrÉþf2%sŠyn›@S°%3ªâ*„à,r@ÊŠk児L8&GnØlEæŸØŠe £ƒÒĵW¾.f¹øq6"‹Q‚WÛ•aK–Cåj†ë8VÆÉ`±×3öêí |ÞƒŸlvÝöŒü¶]ÓŽÍ–|eçGgõu},îðku<‘G—õñ6ûÀÞÌØGø#kY®Mr×"´d¥¹Æ©m0-¯ڱק?Hì—{`´DFA£ÆÆÝ3$—8¾ f®âqjs7Ä(r€€½_¯ï‘ìj„²&2(¹hž¢›aˆÊÉåfþ½ž" °Ûl•÷uàU Š ¿>¬ê±=UùÀ-Noå0[¼K?éÇbñ˜û"[ý‰*Ù$ *ô¼Hh.L(¨ü^ß­ç·¸«V/¡^;#´56۝Õß‘Ír‰¯ýèÙþC‡L‘h+ilÚÊÛó¤í !‹Ñy˜^lŽÇ‰ü„Âí?8'÷››ÛǶړenÿq†eL¢œE®wpbìâɧ6—Ú½t蹕ÇGNÖöœfÿ|{¡cÚŽ˜Œ¡!“Ïk¨A°Á¦õuÊžU}õv^/® )_âĶï`.âqg½ž7//6×ª«¤ìs-£Èÿ.vê‘d³ŠKܤÒTƒd«yjhڝ ¶4sè?iæ¸ôuÊ±uŧÈó=¡\g\iåř$lYxè¢ðDæ¨j‹Q†Û±Ü ™Pdw´¶%ˆ‚À{ˆ¼¬ˆBÿvñ%k¸ä{ðåù»½Þ>`2Üz‚{i¬·*`S8¯+ihóì]tþÏH°â£ù”j*a’Ò?•6Ÿlù(ZlÊQxu‘Úù(š58*SÁFà RzV؈gÔRzV؈gÔRz–,:Õþ(ýéaâJ–^ô0q ˵ãÁªQ-«¶Y‘r½ìXÓÌEÁœ¹ÎM,ƒ˜;Ç1­1)|Ôm@*Q@Ö&²ËÌÒïW’úywjéºOZePÉLõÖ'ÕtIÅ$2i7³³±¦ÆŽÔÔõaº]ñ)úSS×\:ÒºJûSSï20pŸe;åaN˜CíÇ»@á¸WJ(©ý_ *ϵÂ'oÚ;ÁH)âL ãøj‚G­Ø’ØE4ÿ—‡¢Zø°-þ¦©PΟ<7‘é-U°+»7Žò¨úÁÒ“ú¡ŽKM!FÆ4a¡ ±@¨F†œË-‹Ì¢ nReA'Ë ‰ÝGöêÏSɦ·Ã-#á…%´÷¿…ÇPÝtÀV*±&µ¯¼”þg=¾0 endstream endobj 16 0 obj 1067 endobj 17 0 obj <>>>/Group<>>> endobj 18 0 obj <>stream xœSKOÃ0þ+>v‡†$ÍóÈHp@€Š8LF»—Ôa”mü}ì´›Z¨Ž”|ùb;ŸVÇœœÌsÎ¥R²>@„CV‚cš¸!‡´L˜ôÞ;k=Ő SÒa*¥ñˆb†Ì^ êåM¬'“ ¼çÇà~ë$Ó‰Cqþÿ:4¦’sá:â— m˜ådÖ´*¦)\Ü\¢Ý‚®:Wì–0’Y“PAX.gÊ&$ßAZ¢ÃæÑs•ÕÕ$Ñ®™ˆ¨&T4/q›¡/D›#|á½Ù¶AÁ+Puy}t‡EØ.òe>y…ô®ÓA·uôe¶"-,3_ÓsUô°X£‚%´—†4Ó«.Í<’=ú”}ŽòWãXü[¾4ÌiƒØŸWÏÇ%ÏB§2”½'Pž½»'®|;v¿CX>·!¢‚-u¶ï…<> endobj 2 0 obj <> endobj 3 0 obj <> endobj 4 0 obj <>stream 2023-10-11T15:18:00Z 2023-10-18T11:51:00Z Microsoft Office Word application/pdf Michael Stevens Aspose.Words for Cloud for .NET 23.11.0 endstream endobj 20 0 obj 848 endobj 12 0 obj <> endobj 13 0 obj [226 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 507 507 0 0 0 0 0 0 0 0 0 0 0 0 0 0 606 0 0 630 0 459 0 0 0 0 0 0 0 659 676 0 0 563 0 495 653 0 0 0 0 0 0 0 0 0 0 0 494 537 418 537 503 316 0 537 246 0 0 246 813 537 538 0 0 355 399 347 537 473 745] endobj 14 0 obj <> endobj 8 0 obj <> endobj 9 0 obj [226 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 579 0 533 615 0 0 0 0 252 0 0 0 0 646 662 517 0 543 0 487 642 567 0 0 0 0 0 0 0 0 0 0 479 525 423 525 498 305 471 525 229 0 0 229 799 525 527 525 0 349 391 335 525 452 715 0 453] endobj 10 0 obj <> endobj 15 0 obj <>stream xœ}U[ŽÛ0¼Š.`A$EI¯&H¸‡qÝ´kª÷Wà¿G VñVg`½d"%>.[ó~ÆngŒ|Ì´G*ØùAMª®¡³»bÙ£IŒ¶Ê¿÷ã‹6Ü&‹û#tñ.2ëŸ#¡c:'àwùpEÇö@uX(P!OS¦õè¬Ë¦7q{âãˆ]#²…ÄQU_ãÒÙc:ø«Ò ‡æÏÞÈÅdŠ"·vquÒ`o¾–ÚÂrîIÛ5é5¸êñÆ»&„À!„u{fñ\R>Fùñ]fߨᅂv_?ð}ÙQÑFlÔQ¦×>¬˜±/¤¼Î©«å‹Z£wŠ¹Ëb9JudFpÌÆQÞJ¯“Áa,¸ b‹½:šÂ"‡ wˆsm’giòäÃ(èÚt¾fZ–݆Wa–1ŽyÐFí“Y ã@Ôj¾§X›Ó¹1'vlöµ[d¬Ü †;a^ß ¼áÌ=|@ng$'@þnR•ñ L+ŬÕx’>aÒ«ù]Ò˜ק=P‰"HÙ÷Y¸u4f ‹ÕŽ‚ù7ʪ³‘N”SqH a#®uƒÁè8¸GA`†RF9I?ÚÄ­‡æ6Çc`Dtr3*4C`Í$IIßeë „¤Îé9b¯Áªá)-›e«¥ZÞ>6éÖËæu.šE퍱²ñ¾0šìR\¹§FíÅ£(ô*v‚nèHÿw¥¹e£¼j“…ê(„c¸gÇ-àÁÍs›DÇÒ‰¤pL¨5é@>stream xœí} |[ŵ÷Ü{µX»ä}÷µeË‹ly—¬òË{¼›àIJ,Ç"¶e$9N´¥@)´P(åR  E ` JSº--éF7 ôµPL)¥[ÀÖwfΕlgáñèûÞ÷ÞïÃÉÑæÌÌ™9Ëœ™{…á!:r€¤§»¯´â¶7ëróK ×”sFáP^E·HëÚ7=2ø:!|+!ŠkÆgvN}ᵆ{ ‰yŽUÜÎɽãß¾³ŠÓ+„da&Üα­wÿ°‚ÊAW=Ý£B3Ôo€zîÄT`Ïž+Õ P?õ=“^—“ðÊ^BªŠ¡~Ù”sÏŒî«én¨êâ´sÊm»î6¨‡ò®¯?N#°¾ñdÚ>ãsÏüÓþ=?Ôk I|‰²6þ‘“ù­òJÐ QxŽ<“Âe”¼ “ ¼ìER~‚ä^ RT@¤³O‰HÈRXA–wRy;o ¦mÂr=ÄÃ'av£Ô²ø2’Ÿå¤‚Ô“&ÒBºH7é%ýd€8É(q‘1â&ãd‚xÈ$™"ÓÄK|ÄOd–ì&sá0“ýG‡_¿```

So what I am asking is if I am missing something or doing something wrong. I want to remind you this is the exact same file that works in the demo.

Thank you very much!

@PLA-Antreas

Please note that convertDocument returns the output file in the response; you can save it locally as follows:

....
$file = $wordsApi->ConvertDocument($request);	
copy($file->getPathName(),"DOCXtoPDF.pdf");
....

Apologies I forgot to reply and mention that that was indeed the issue. A clear oversight from my end. Thank you very much!

1 Like