/** * Puts transferred HTML-code to temporary files. * @param string $folder Path to the temporary files folder. * @param array[Object] $html An array with HTML-code of pages. * @return array[string] An array of created temporary file names. */ function buildHTMLFiles($folder, $html) { $result = array(); $i = 1; foreach ($html as $content) { $file = "{$folder}/" . buildUniqueFileName('printHtml-' . $i, '.html'); $fh = fopen($file, 'w'); //check if file is writable if (is_writable($file)) { fwrite($fh, $content['html']); fclose($fh); $result[] = basename($file); } else { throw new Exception('Can\'t create or read file.'); } } return $result; }
// if you change OUTPUT_PATH then URL string pointing to that folder should be placed here. // By default it's set to this script URL w/o 'server.php' ending //define('OUTPUT_URL', 'http://localhost/somefolder/'); define('OUTPUT_URL', getCurrentPageURL(true)); try { if (!isset($_POST['html'])) { throw new Exception('Error in request data.'); } // get request parameters $html = json_decode($_POST['html'], true); $format = stripslashes($_POST['format']); $orientation = stripslashes($_POST['orientation']); $range = stripslashes($_POST['range']); $fileFormat = stripslashes($_POST['fileFormat']); // generate name of the created single PDF file $out = $range . buildUniqueFileName('-exportedPanel', '.') . $fileFormat; // output file full path $outputFilePath = OUTPUT_PATH . "/{$out}"; // output file URL $outputFileURL = OUTPUT_URL . $out; // put transferred pages HTML code to temporary files $files = buildHTMLFiles(OUTPUT_PATH, $html); // render HTML files to PNG images $images = renderHTMLFiles($files, OUTPUT_PATH, OUTPUT_URL, $format, $orientation); // build single PDF/PNG output file convertImagesToFile($images, $outputFilePath, $fileFormat); // cleanup temporary png/html files removeFiles(array_merge($images, $files), OUTPUT_PATH); header('Content-Type: application/json; charset=utf-8'); //header("access-control-allow-origin: *"); // return response