Convert HTML Document to PDF in PHP

Hi

I am having same issue, using php to convert html to PDF

example code

SaasposeApp::$OutPutLocation =SERVER_FILE_PATH.‘media/candidate/pdf/’;

$mainDocumentFile = SERVER_FILE_PATH.’/lib/pdf_converter/test.html’;
$report=‘purchase_reports.pdf’;
try{
//create document object
$pdfDocument = new Document("");
//create PDF from HTML
$result = $pdfDocument->CreateFromHtml($report, $mainDocumentFile);

if($result == “”)
echo “Pdf file has been created successfully”;
}
catch (Exception $e) {
throw new Exception($e->getMessage());
}

could you please help to solve this issue ?

Hi Simon,

We are sorry for the inconvenience caused. I am afraid I am unable to notice any cloud service issue while converting HTML document to PDF in PHP using Aspose Cloud default storage. We will appreciate it if you please share the exact error message and also confirm which storage option you are using. It will help us to investigate the issue.

Best Regards,

Hi Simon,


In addition to above reply. Please also share your sample HTML file for testing.

Best Regards,

@simon12

As an update, please note we have made several changes in the Aspose.PDF Cloud API. The latest API version of Aspose.PDF Cloud uses JSON Web Token(JWT) for a request authorization and provides Storage API methods for cloud storage operations. Kindly find the sample code to convert HTML document to PDF in PHP and check Aspose.PDF Cloud SDK for PHP as well.

Steps to Convert HTML to PDF without Losing Formatting in PHP

  1. Create a Free Account with Aspose.Cloud and get credentials
  2. Install Aspose.PDF Cloud SDK for PHP from Packagist
  3. Create a PHP script file for the code
  4. Add reference of Aspose.PHP Cloud SDK for PHP
  5. Initialize the API and Upload HTML Document to Cloud Storage using UploadFile API Method
  6. Convert HTML document to PDF with GetHtmlInStorageToPdf API method and get the PDF document in response

Convert HTML to PDF without Losing Formatting in PHP

<?php
require __DIR__.'\..\vendor\autoload.php';

use Aspose\PDF\Api\PdfApi;
use Aspose\PDF\Configuration;

// Get Client ID and Secret from https://www.aspose.cloud
$cliedId = 'xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx';
$clientSecret = 'xxxxxxxxxxxxxxxxxxx';

$tempFolder = null;

$config = new Configuration();
$config->setAppKey($clientSecret);
$config->setAppSid($cliedId);

// Initialize the API
$pdfApi = new PdfApi(null, $config);

// Upload file to cloud stoage
$localFile = "HtmlWithImage.zip";
$name = "HtmlWithImage.zip";
$pdfApi->uploadFile($localFile, $name);

// Convert HTML to PDF
$html_file_name = "HtmlWithImage.html";
$height = 650;
$width = 250;
$src_path = $tempFolder . '/' . $name;

$response = $pdfApi->getHtmlInStorageToPdf($src_path, $html_file_name, $height, $width);
echo $response

?>