Convert Word Document to PDF from Azure Cloud Storage in PHP and Download

URL returned by PHP file after conversion of the document.
https://api.groupdocs.cloud/v2.0/conversion/storage/file/converted/docssample.pdf

The error I’m getting accessing that document is as followes
{“requestId”:“c4d569d0-5dcb-4f96-b9f1-e73cf9c96da9”,“error”:{“code”:“error”,“message”:“ClientId is undefined. Please check authorization.”,“description”:“Operation Failed. General Error.”,“dateTime”:“2021-02-25T06:30:24.2486833Z”,“innerError”:null}}

can someone please explain how can I access that using PHP?
Thank you
Gopal

@gopalsharma

Please check the sample code to convert Word document to PDF from Azure Cloud Storage in PHP and download output PDF document using GroupDocs.Conversion Cloud SDK for PHP.

<?php

require_once('D:\xampp\htdocs\groupdocs-conversion-cloud-php-master\vendor\autoload.php');
//require_once(__DIR__ . '/vendor/autoload.php');

//Get Client Id and Client Secret from https://dashboard.groupdocs.cloud
$myClientId = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx";
$myClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Create instance of the API
$configuration = new GroupDocs\Conversion\Configuration();
$configuration->setAppSid($myClientId);
$configuration->setAppKey($myClientSecret);
$convertApi = new GroupDocs\Conversion\ConvertApi($configuration); 
$fileApi = new GroupDocs\Conversion\FileApi($configuration); 

try {
    	// Prepare convert settings
	$settings = new GroupDocs\Conversion\Model\ConvertSettings();
	$settings->setFilePath("02_pages.docx");
	$settings->setFormat("pdf");
	$settings->setOutputPath("converted");
	$settings->setStorageName("myStorage");


	// Convert
	$result = $convertApi->convertDocument(new GroupDocs\Conversion\Model\Requests\ConvertDocumentRequest($settings));

	// Download
	$request = new GroupDocs\Conversion\Model\Requests\downloadFileRequest("converted/02_pages.pdf",null,null);
	$response = $fileApi->downloadFile($request);
	copy($response->getPathName(),"02_pages.pdf");

} catch (Exception $e) {
    echo  "Something went wrong: ",  $e->getMessage(), "\n";
    PHP_EOL;
}

?>

@tilal.ahmad

I’m using it on a Linux OS using an apache web server.
where it gets downloads?
I can’t seem to find it, there must be a seamless way for a user to download it and access it, just like what when download a normal file browser.

it’s little urgent.

Thank you,
Gopal

@gopalsharma

// Download
	$request = new GroupDocs\Conversion\Model\Requests\downloadFileRequest("converted/02_pages.pdf",null,null);
	$response = $fileApi->downloadFile($request);
	copy($response->getPathName(),"02_pages.pdf");

It downloads the file to the same directory where your PHP file exists. If you want to download it to some other place you can define the path accordingly. For example, following path will download it to my browser download path.

copy($response->getPathName(),"C:/Users/tilal/Downloads/02_pages.pdf");

@tilal.ahmad
Thank you for responding back.
Let me explain to you what’s happening when a user clicks on a link having a filename, which is the same on the azure storage.
Document get converted to .pdf with watermark in “converted” folder and then I add some security using “viewer” api , security gets added to document and again copying it to “viewer/samle_pdf/sample.pdf”.

And finally downloading and copying it to my webserver “/var/www/datafolder/sample.pdf

from my webserver to user location.

This seems to be a long and time-consuming process.

In short, I just need to add some watermark and security, and download it(converted document) user’s location without the creation of any replica.
Thank you so much for your help.

Gopal

@gopalsharma

You may convert, add watermark and add security with GroupDocs.Viewer REST API as well. Please check the sample PHP code for reference.

<?php
require_once('D:\xampp\htdocs\groupdocs-viewer-cloud-php-master\vendor\autoload.php');
//require_once(__DIR__ . '/vendor/autoload.php');
//Get Client Id and Client Secret from https://dashboard.groupdocs.cloud
$myClientId = "xxxxx-xxxx-xxxx-xxxxx-xxxxxxxxxx";
$myClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Create instance of the API
$configuration = new GroupDocs\Viewer\Configuration();
$configuration->setAppSid($myClientId);
$configuration->setAppKey($myClientSecret);
$viewerApi = new \GroupDocs\Viewer\ViewApi($configuration);
//$apiInstance# new \GroupDocs\Viewer\ViewApi($configuration);

try {
	$viewOptions = new GroupDocs\Viewer\Model\ViewOptions();
	$fileInfo = new GroupDocs\Viewer\Model\FileInfo();
	$fileInfo->setFilePath("Temp/Revisions.docx");
	$viewOptions->setFileInfo($fileInfo);
	$viewOptions->setViewFormat(GroupDocs\Viewer\Model\ViewOptions::VIEW_FORMAT_PDF);
	$renderOptions = new GroupDocs\Viewer\Model\PdfOptions();
	$renderOptions->setPermissions(GroupDocs\Viewer\Model\PdfOptions::PERMISSIONS_DENY_MODIFICATION);
	$renderOptions->setPermissionsPassword("p123");
	$renderOptions->setDocumentOpenPassword("o123");
	$viewOptions->setRenderOptions($renderOptions);
	$watermark = new GroupDocs\Viewer\Model\Watermark();
	$watermark->setText("This is a watermark");
	$viewOptions->setWatermark($watermark);

	$request = new GroupDocs\Viewer\Model\Requests\CreateViewRequest($viewOptions);
	$response = $viewerApi->createView($request);
	print_r($response);

} catch (Exception $e) {
    echo  "Something went wrong: ",  $e->getMessage(), "\n";
    PHP_EOL;
}

?>

@tilal.ahmad
in above code
print_r($response);

Returns following and don’t download the converted file.

GroupDocs\Viewer\Model\ViewResult Object ( [container:protected] => Array ( [pages] => [attachments] => Array ( ) [file] => GroupDocs\Viewer\Model\Resource Object ( [container:protected] => Array ( [path] => viewer/ViewSyllabus_docx/ViewSyllabus.pdf [downloadUrl] => https://api.groupdocs.cloud/v2.0/viewer/storage/file/viewer/ViewSyllabus_docx/ViewSyllabus.pdf [0] => Resource ) ) ) ) 

Even the downloadUrl is not directly accessible.

Thank you,
Gopal

@gopalsharma

As already stated above you need to download file using downlaodFile method of respective GroupDocs Cloud API. Please check sample file download code using GroupDocs.Viewer Cloud SDK for PHP.

// Download
.....
	$request = new GroupDocs\Viewer\Model\Requests\downloadFileRequest("ViewSyllabus_docx/ViewSyllabus.pdf",null,null);
	$response = $fileApi->downloadFile($request);
	copy($response->getPathName(),"ViewSyllabus.pdf");
1 Like

A post was split to a new topic: Protect PDF document in Python using GroupDocs.Viewer REST API

2 posts were split to a new topic: How to delete a file from azure storage in PHP