/** * Converts a URL to a PDF using pdfonline.com service. * @param String $url url which a PDF should be generated for. * @param String $outputFilePath Path for the PDF to generate. * @param String $errorMessage Error message when PDF generation is unsuccessful. * @return true if PDF generation was successful; false if not. */ function convertToPdf($url, $outputFilePath, &$errorMessage) { if (!mkFileDirs($outputFilePath)) { $errorMessage = "Unable to create output directory for {$outputFilePath}."; return false; } $credentials = getPdfOnlineCredentials(); $requestParameters = array("cURL" => $url, "username" => $credentials['username'], "page" => "0", "top" => "0.5", "bottom" => "0.5", "left" => "0.5", "right" => "0.5"); // This url was found from viewing the source of: // "http://savepageaspdf.pdfonline.com/pdfonline/pdfonline.asp?" . . http_build_query($requestParameters) // except using the author_id instead of the username. // There is an iframe that uses the url below for downloading the PDF. $createPdfUrl = "http://savepageaspdf.pdfonline.com/pdfonline/pdfoMaster.asp?" . http_build_query($requestParameters); copy($createPdfUrl, $outputFilePath); if (!file_exists($outputFilePath) || !filesize($outputFilePath)) { $errorMessage = "There was a problem creating the PDF."; return false; } // If pdfonline.com has a problem generating a PDF, it will return an HTML file instead of a PDF. // Example error line: "Unable to convert URL. Error message:Unable to open input documentURL: URL_GOES_HERE" $outputFileResource = fopen($outputFilePath, "r"); $firstLine = fgets($outputFileResource); fclose($outputFileResource); if (strpos($firstLine, "Unable to convert URL") !== false) { $errorMessage = $firstLine; return false; } return true; }
/** * Converts either HTML or a URL to a PDF using the htm2pdf.co.uk web service. * This was taken from: http://forum.htm2pdf.co.uk/viewtopic.php?f=4&t=9 * @param Array $params Will be passed to the htm2pdf.co.uk. * Must include the key "html" or "aUrl", and then corresponding values. * @param String $outputFilePath Path for the PDF to generate. * @param String $errorMessage Error message when PDF generation is unsuccessful. * @return true if PDF generation was successful; false if not. */ function convertToPdf($params, $outputFilePath, &$errorMessage) { if (!mkFileDirs($outputFilePath)) { $errorMessage = "Unable to create output directory for {$outputFilePath}."; return false; } // Add API key to params. $credentials = getHtm2PdfCredentials(); $params['key'] = $credentials['key']; // Check if SOAP is available. if (!class_exists('SoapClient')) { $errorMessage = "Unable to create SOAP client as SoapClient class doesn't exist."; return false; } // Client for pdf creation. $client = new SoapClient('http://webservice.htm2pdf.co.uk/htm2pdf.asmx?wsdl'); // Check remaining credits. $checkCredits = true; if ($checkCredits) { try { $credits = $client->NumberOfCredits($params)->NumberOfCreditsResult; if ($credits == 0) { $errorMessage = 'No HTM2PDF credits remaining.'; return false; } elseif ($credits < 100) { mail("*****@*****.**", "htm2pdf is low on credits", "There are only {$credits} credits available."); } } catch (Exception $e) { // credit count couldn't be retrieved... oh well!! // this is a non-essential error, so keep on going } } $outputFile = fopen($outputFilePath, 'w'); $data; try { if (isset($params['html'])) { $data = $client->Htm2PdfDoc($params)->Htm2PdfDocResult; } elseif (isset($params['aUrl'])) { $data = $client->Url2PdfDoc($params)->Url2PdfDocResult; } else { $errorMessage = print_r($params, true) . " doesn't include 'html' or 'aUrl'"; return false; } } catch (Exception $e) { $errorMessage = 'Error getting PDF. (' . $e->getMessage() . ')'; return false; } fwrite($outputFile, $data); fclose($outputFile); if (!file_exists($outputFilePath) || !filesize($outputFilePath)) { $errorMessage = 'There was a problem creating the PDF.'; return false; } return true; }
$html = get_magic_quotes_gpc() ? stripslashes($_POST["html"]) : $_POST["html"]; // quickbook_item_supplements $sizeKey = "size"; $productTypeKey = "productType"; $query = createSqlQuery("SELECT qbis.size as '{$sizeKey}'", ", qbis.product_type as '{$productTypeKey}'", "FROM quickbooks_item_supplements qbis", "WHERE qbis.id LIKE '{$idBase}-%'"); $result = queryDb($query); if (mysql_num_rows($result) == 0) { // quickbooks_item_supplements with this id doesn't exist echo "No information for products with id base: {$idBase}."; return; } $row = mysql_fetch_assoc($result); $productType = $row[$productTypeKey]; $size = $row[$sizeKey]; $htmlForPdfPath = getHtmlForPdfPath($productType, $size); mkFileDirs($htmlForPdfPath); file_put_contents($htmlForPdfPath, $html); $htmlForPdfUrl = "http://" . $_SERVER['SERVER_NAME'] . "/" . getDirectoryPathFromRoot(__FILE__) . "/{$htmlForPdfPath}"; $pdfPath = getPdfPath($productType, $size); $tmpPdfPath = $pdfPath . ".tmp"; if (!convertToPdf($htmlForPdfUrl, $tmpPdfPath, $errorMessage)) { echo implode("\n", array("PDF generation failed because: {$errorMessage}", "PDF path: {$tmpPdfPath}", "PDF HTML URL: {$htmlForPdfUrl}", "HTML: {$html}")); unlink($tmpPdfPath); return; } if (file_exists($pdfPath)) { $archivedPdfPath = getArchivedPdfPath($productType, $size); mkFileDirs($archivedPdfPath); rename($pdfPath, $archivedPdfPath); } rename($tmpPdfPath, $pdfPath);