dsoukup Posted February 17, 2009 Share Posted February 17, 2009 I apologize for the newbie question...but here goes. I would like to implement the ability to create PDF files of my dynamically-generated pages. The pages are "dynamically generated" with content for a mySQL database. To implement the PDF function, I am trying to use html2ps and html2pdf scripts (see http://www.tufat.com/s_html2ps_html2pdf.htm if you are unfamilar). In essence, I need to pass some variables from the actual page (laudes2.php) to the page that generates the PDF (output_laudes.php). The page I am actually trying to convert to PDF is laudes2pdf.php which is essentially a copy of laudes2.php with some formatting differences. Since the html2pdf scripts do not work with $_SESSIONS well from what I have read, I am try to pass the variables through the URL and $_GET. Since some of the variables I am passing are strings with the possibility of special characters, I use urlencode to encode the "?...." portion of the URL. Below are the relevant snippets of code: <?php session_start(); $_SESSION['page'] = "laudes2.php"; #various non-relevant items, mostly HTML $query = sprintf("SELECT * FROM laudes_".$_SESSION['date'][year]." WHERE yday=%d", mysql_real_escape_string($_SESSION['date'][yday]+1)); $res = mysql_query($query); $row = mysql_fetch_array($res); $query2 = sprintf("SELECT * FROM calendarium_".$_SESSION['date'][year]." WHERE yday=%d", mysql_real_escape_string($_SESSION['date'][yday]+1)); $res2 = mysql_query($query2); $row2 = mysql_fetch_array($res2); $feast = $row2["feast"]; $class = $row2["class"]; $ant1 = $row["ant1"]; $ant2 = $row["ant2"]; $ant3 = $row["ant3"]; $ant4 = $row["ant4"]; $ant5 = $row["ant5"]; $ps1 = $row["ps1"]; $ps2 = $row["ps2"]; $ps3 = $row["ps3"]; $ps4 = $row["ps4"]; $ps5 = $row["ps5"]; $capit = $row["capit"]; $hymnus = $row["hymnus"]; $v = $row["v"]; $r = $row["r"]; $ant_ben = $row["ant_ben"]; $oratio = $row["oratio"]; if($row["com1_oratio"] == NULL) { $com1 = 0; $com1_title = ""; $com1_v = ""; $com1_r = ""; $com1_ant_ben = ""; $com1_oratio = 0; } else { $com1 = 1; $com1_title = $row2["com1"]; $com1_v = $row["com1_v"]; $com1_r = $row["com1_r"]; $com1_ant_ben = $row["com1_ant_ben"]; $com1_oratio = $row["com1_oratio"]; } if($row["com2_oratio"] == NULL) { $com2 = 0; $com2_title = ""; $com2_v = ""; $com21_r = ""; $com2_ant_ben = ""; $com2_oratio = 0; } else { $com2 = 1; $com2_title = $row2["com2"]; $com2_v = $row["com2_v"]; $com2_r = $row["com2_r"]; $com2_ant_ben = $row["com2_ant_ben"]; $com2_oratio = $row["com2_oratio"]; } mysql_free_result($res); mysql_free_result($res2); #various non-relevant items, mostly HTML $encoded_url = urlencode("?month=".$_SESSION['date'][mon]."&day=".$_SESSION['date'][mday]."&year=".$_SESSION['date'][year]."&yday=".$_SESSION['date'][yday]."&feast=".$feast."&class=".$class."&ant1=".$ant1."&ant2=".$ant2."&ant3=".$ant3."&ant4=".$ant4."&ant5=".$ant5."&ps1=".$ps1."&ps2=".$ps2."&ps3=".$ps3."&ps4=".$ps4."&ps5=".$ps5."&capit=".$capit."&hymnus=".$hymnus."&v=".$v."&r=".$r."&ant_ben=".$ant_ben."&oratio=".$oratio); echo "<p class=\"psalm_title\">Download to PDF:<a href=\"http://www.officiumdivinum.org/public_html/samples/output_laudes.php".$encoded_url."\"><img src=\"http://www.officiumdivinum.org/graphics/pdf.gif\" alt=\"PDF\"></a></p>"; #the rest of the script is below which actually builds the main/actual page ?> <?php /** * Thanks for JensE for providing the code of fetcher class */ require_once(dirname(__FILE__).'/../config.inc.php'); require_once(HTML2PS_DIR.'pipeline.factory.class.php'); error_reporting(E_ALL); ini_set("display_errors","1"); @set_time_limit(10000); parse_config_file(HTML2PS_DIR.'html2ps.config'); /** * Handles the saving generated PDF to user-defined output file on server */ class MyDestinationFile extends Destination { /** * @var String result file name / path * @access private */ var $_dest_filename; function MyDestinationFile($dest_filename) { $this->_dest_filename = $dest_filename; } function process($tmp_filename, $content_type) { copy($tmp_filename, $this->_dest_filename); } } class MyFetcherMemory extends Fetcher { var $base_path; var $content; function MyFetcherMemory($content, $base_path) { $this->content = $content; $this->base_path = $base_path; } function get_data($url) { if (!$url) { return new FetchedDataURL($this->content, array(), ""); } else { // remove the "file:///" protocol if (substr($url,0,=='file:///') { $url=substr($url,; // remove the additional '/' that is currently inserted by utils_url.php if (PHP_OS == "WINNT") $url=substr($url,1); } return new FetchedDataURL(@file_get_contents($url), array(), ""); } } function get_base_url() { return 'file:///'.$this->base_path.'/dummy.html'; } } /** * Runs the HTML->PDF conversion with default settings * * Warning: if you have any files (like CSS stylesheets and/or images referenced by this file, * use absolute links (like http://my.host/image.gif). * * @param $path_to_html String HTML code to be converted * @param $path_to_pdf String path to file to save generated PDF to. * @param $base_path String base path to use when resolving relative links in HTML code. */ function convert_to_pdf($html, $path_to_pdf, $base_path='') { $pipeline = PipelineFactory::create_default_pipeline('', // Attempt to auto-detect encoding ''); // Override HTML source // @TODO: default http fetcher will return null on incorrect images // Bug submitted by 'imatronix' (tufat.com forum). $pipeline->fetchers[] = new MyFetcherMemory($html, $base_path); // Override destination to local file $pipeline->destination = new DestinationBrowser('test.pdf'); $baseurl = ''; $media =& Media::predefined('A4'); $media->set_landscape(false); $media->set_margins(array('left' => 0, 'right' => 0, 'top' => 0, 'bottom' => 0)); $media->set_pixels(1024); global $g_config; $g_config = array( 'cssmedia' => 'screen', 'scalepoints' => '1', 'renderimages' => true, 'renderlinks' => true, 'renderfields' => true, 'renderforms' => false, 'mode' => 'html', 'encoding' => '', 'debugbox' => false, 'pdfversion' => '1.4', 'draw_page_border' => false ); $pipeline->configure($g_config); $pipeline->process_batch(array($baseurl), $media); } $encoded_url = urlencode("?month=".$_GET['mon']."&day=".$_GET['mday']."&year=".$_GET['year']."&yday=".$_GET['yday']."&feast=".$_GET['feast']."&class=".$_GET['class']."&ant1=".$_GET['ant1']."&ant2=".$_GET['ant2']."&ant3=".$_GET['ant3']."&ant4=".$_GET['ant4']."&ant5=".$_GET['ant5']."&ps1=".$_GET['ps1']."&ps2=".$_GET['ps2']."&ps3=".$_GET['ps3']."&ps4=".$_GET['ps4']."&ps5=".$_GET['ps5']."&capit=".$_GET['capit']."&hymnus=".$_GET['hymnus']."&v=".$_GET['v']."&r=".$_GET['r']."&ant_ben=".$_GET['ant_ben']."&oratio=".$_GET['oratio']); convert_to_pdf(file_get_contents("http://www.officiumdivinum.org/laudes2pdf.php".$encoded_url), "c:\test.pdf"); ?> <?php session_start(); #various non-relevant items, mostly HTML $feast = $_GET['feast']; $class = $_GET['class']; $ant1 = $_GET['ant1']; $ant2 = $_GET['ant2']; $ant3 = $_GET['ant3']; $ant4 = $_GET['ant4']; $ant5 = $_GET['ant5']; $ps1 = $_GET['ps1']; $ps2 = $_GET['ps2']; $ps3 = $_GET['ps3']; $ps4 = $_GET['ps4']; $ps5 = $_GET['ps5']; $capit = $_GET['capit']; $hymnus = $_GET['hymnus']; $v = $_GET['v']; $r = $_GET['r']; $ant_ben = $_GET['ant_ben']; $oratio = $_GET['oratio']; if($_GET['com1'] != 0) { $com1 = 1; $com1_title = $_GET['com1_title']; $com1_v = $_GET['com1_v']; $com1_r = $_GET['com1_r']; $com1_ant_ben = $_GET['com1_ant_ben']; $com1_oratio = $_GET['com1_oratio']; } if($_GET['com2'] != 0) { $com2 = 1; $com2_title = $_GET['com2_title']; $com2_v = $_GET['com2_v']; $com2_r = $_GET['com2_r']; $com2_ant_ben = $_GET['com2_ant_ben']; $com2_oratio = $_GET['com2_oratio']; } #then the actual page is built using the variables above. a type example of the code used is listed below: echo "<h2>".$feast.", Classis: ".$class."</h2>"; if($com1 == 1) { echo "<h3>Commemoratio: ".$com1_title."</h3>"; } if($com2 == 1) { echo "<h3>Commemoratio: ".$com2_title."</h3>"; } #various non-relevant items, mostly HTML When you click on the PDF link in laudes2.php, it should pass the variables to output_laudes.php, when then essentially passes those same variables to laudes2pdf.php in order to build the PDF. However, I always get a 403 FORBIDDEN error when I use the urlencode() function. If I do not use the urlencode() function, I get several errors in output_laudes.php. If I do not use urlencode(), I can access ".../laudes2pdf.php?mon=2&mday=17&year=2009&mday=47&feast=Feria&class=4&ant1=Dele iniquitatem meam,..." just fine. However, if I try to access laudes2pdf.php via an encoded URL such as "/laudes2pdf.php?month=2&day=17&year=2009&yday=47&feast=Feria&class=4&ant1=Dele+iniquitatem+meam," I get the 403 FORBIDDEN error. What am I doing wrong? Link to comment https://forums.phpfreaks.com/topic/145621-403-forbidden-with-urlencode-_get/ Share on other sites More sharing options...
farkewie Posted February 18, 2009 Share Posted February 18, 2009 Hi, Have you considered writing it to a temporary html file? then passing that file to your pdf script? http://www.macronimous.com/resources/Converting_HTML2PDF_using_PHP.asp <? require('html2fpdf.php'); $pdf=new HTML2FPDF(); $pdf->AddPage(); $fp = fopen("sample.html","r"); $strContent = fread($fp, filesize("sample.html")); fclose($fp); $pdf->WriteHTML($strContent); $pdf->Output("sample.pdf"); echo "PDF file is generated successfully!"; ?> Link to comment https://forums.phpfreaks.com/topic/145621-403-forbidden-with-urlencode-_get/#findComment-764716 Share on other sites More sharing options...
dsoukup Posted February 18, 2009 Author Share Posted February 18, 2009 Have you considered writing it to a temporary html file? then passing that file to your pdf script? I attempted using html2fdpf.php as you suggested, but I tried to convert my page while still dynamic .php. This resulted in a PDF that essentially displayed all the PHP code. You mentioned I should write the PHP page I want to convert to a "temporary html file." Due to my inexperience with PHP, I did several searches on doing this and found several sites that suggested using .htaccess to convert a dynamic URL to a static .htm URL. This does not appear to fulfill what you suggested. Do you have any other resources or tips you could point me to? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/145621-403-forbidden-with-urlencode-_get/#findComment-765279 Share on other sites More sharing options...
farkewie Posted February 19, 2009 Share Posted February 19, 2009 Hi, Ok so ive had a play and here is the code to write the HTML file to disk. // Replace this with your output. $page = '<h2>This is a H2 header!!</h2> This is some randome text <br />and more on a new line'; $fp = fopen("tempFile.html","w"); fwrite($fp,$page); fclose($fp); I have tested this and it writes a html file. Link to comment https://forums.phpfreaks.com/topic/145621-403-forbidden-with-urlencode-_get/#findComment-765976 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.