Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Last active February 8, 2022 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/9864a86f27d7ed22ef42cdeaa39a1b85 to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/9864a86f27d7ed22ef42cdeaa39a1b85 to your computer and use it in GitHub Desktop.
Display Word Document in HTML Page using a REST API in PHP
// This code example demonstrates how to add client Id and Secret in the code.
static $ClientId = '659fe7da-715b-4744-a0f7-cf469a392b73';
static $ClientSecret = 'b377c36cfa28fa69960ebac6b6e36421';
static $ApiBaseUrl = 'https://api.groupdocs.cloud';
static $MyStorage = '';
// Intializing the configuration
$configuration = new GroupDocs\Viewer\Configuration();
// Seting the configurations
$configuration->setAppSid(self::$ClientId);
$configuration->setAppKey(self::$ClientSecret);
$configuration->setApiBaseUrl(self::$ApiBaseUrl);
// This code example demonstrates how to download the rendered HTML pages from the cloud.
$fileApi = new GroupDocs\Viewer\FileApi($configuration);
// Get all the pages
$pages = $response->getPages();
// Save pages one by one
foreach ($pages as $page)
{
// Create download file request
$downloadFileRequest = new GroupDocs\Viewer\Model\Requests\DownloadFileRequest($page->getPath(), "");
// Download file
$file = $fileApi->DownloadFile($downloadFileRequest);
echo "$page downloaded!";
echo "\n";
}
// This code example demonstrates how to render DOCX to HTML.
// Initialize the api
$apiInstance = new GroupDocs\Viewer\ViewApi($configuration);
$fileApi = new GroupDocs\Viewer\FileApi($configuration);
// Define ViewOptions
$viewOptions = new Model\ViewOptions();
// Input file path
$fileInfo = new Model\FileInfo();
$fileInfo->setFilePath("input.docx");
$viewOptions->setFileInfo($fileInfo);
// Set ViewFormat
$viewOptions->setViewFormat(Model\ViewOptions::VIEW_FORMAT_HTML);
// Define HTML options
$renderOptions = new Model\HtmlOptions();
// Set it to be responsive
$renderOptions->setIsResponsive(true);
// Set for printing
$renderOptions->setForPrinting(true);
// Assign render options
$viewOptions->setRenderOptions($renderOptions);
// Create view request
$request = new Requests\CreateViewRequest($viewOptions);
// Create view
$response = $apiInstance->createView($request);
// Load an existing HTML file
$domDoc = new DOMDocument();
$domDoc->loadHTMLFile("C:\Files\Viewer\Sample.html");
$body = $domDoc->GetElementsByTagName('body')->item(0);
// Get pages
$pages = $response->getPages();
// Embed all rendered HTML pages into body tag of existing HTML
foreach ($pages as $page)
{
// Create download file request
$downloadFileRequest = new GroupDocs\Viewer\Model\Requests\DownloadFileRequest($page->getPath(), "");
// Download converted page
$file = $fileApi->DownloadFile($downloadFileRequest);
// Read HTML from download file
$html = file_get_contents($file->getRealPath());
//Add content to fragment
$fragment = $domDoc->createDocumentFragment();
$fragment->appendXML("<div>$html</div>");
// Append the element to body
$body->appendChild($fragment);
}
// Save updated HTML
$output = $domDoc->saveHTML();
// Save the file
file_put_contents("C:\Files\Viewer\Sample.html", $output);
// This code example demonstrates how to render DOCX to HTML.
// Initialize the api
$apiInstance = new GroupDocs\Viewer\ViewApi($configuration);
// Define ViewOptions
$viewOptions = new Model\ViewOptions();
// Input file path
$fileInfo = new Model\FileInfo();
$fileInfo->setFilePath("input.docx");
$viewOptions->setFileInfo($fileInfo);
// Set ViewFormat
$viewOptions->setViewFormat(Model\ViewOptions::VIEW_FORMAT_HTML);
// Define HTML options
$renderOptions = new Model\HtmlOptions();
// Set it to be responsive
$renderOptions->setIsResponsive(true);
// Set for printing
$renderOptions->setForPrinting(true);
// Assign render options
$viewOptions->setRenderOptions($renderOptions);
// Create view request
$request = new Requests\CreateViewRequest($viewOptions);
// Create view
$response = $apiInstance->createView($request);
// Done
echo "HtmlViewerResponsiveLayout completed: ", count($response->getPages());
echo "\n";
// This code example demonstrates how to render Word in HTML with Watermark.
// Initialize the api
$apiInstance = new GroupDocs\Viewer\ViewApi($configuration);
// Define ViewOptions
$viewOptions = new Model\ViewOptions();
// Input file path
$fileInfo = new Model\FileInfo();
$fileInfo->setFilePath("input.docx");
$viewOptions->setFileInfo($fileInfo);
// Set ViewFormat
$viewOptions->setViewFormat(Model\ViewOptions::VIEW_FORMAT_HTML);
// Define Watermark
$watermark = new Model\Watermark();
$watermark->setText("This is sample text watermark!");
$watermark->setSize(100);
$watermark->setColor("Red");
$viewOptions->setWatermark($watermark);
// Create view request
$request = new Requests\CreateViewRequest($viewOptions);
// Create view
$response = $apiInstance->createView($request);
// Done
echo "AddWatermark completed: ", count($response->getPages());
echo "\n";
// This code example demonstrates how to upload a DOCX file to the cloud.
// Initialize the api
$apiInstance = new GroupDocs\Viewer\FileApi($configuration);
// Input file path
$file = "C:\\Files\\Viewer\\input.docx";
// Upload file request
$request = new GroupDocs\Viewer\Model\Requests\uploadFileRequest("input.docx", $file);
// Upload file
$response = $apiInstance->uploadFile($request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment