save html to external file .doc .docx or html

teldrive 29 Aug, 2014
Hi
I apreciate if someone can help on this to save HTML code using for example "custom code action" to an external file with format .doc .docx or HTML , may be simple but i haven't found how can i do inside CFv5
teldrive 29 Aug, 2014
for example i used this php code, but it returns a non readable file 😲
<?php
	header("Content-type: application/vnd.ms-word");
	header("Content-Disposition: attachment; Filename=SaveAsWordDoc.doc");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">
<title>Saves as a Word Doc</title>
</head>
<body>
<h1>Header</h1>
  This text can be seen in word
<ul>
<li>List 1</li>
<li>List 2</li>
</ul>
</body>
</html>
GreyHead 30 Aug, 2014
Hi teldrive,

Here's an extract from some code I wrote a few years ago for a CFv3 application. It should work OK on CFv5 without too many changes
<?php
ob_start();
?>

<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN'
  'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
    <head>
        <meta http-equiv='Content-type' content='text/html; charset=utf-8' />
    <title>Joe title</title>
        <style type='css/text'>. . .</style>

// more HTML here
<?php
$content = ob_get_contents();
ob_end_clean();
$form_path = JPATH_SITE.DS.'components'.DS.'com_chronocontact'.DS.'includes'.DS.'gedeelte_h8'.DS;
$report_name = 'lsr_report_'.$lijst_id.'_'.date('Ymd').'.doc';
$fp = fopen($form_path.'reports'.DS.$report_name, 'w') or die("can't open file");
fwrite($fp, $content);
fclose($fp);
echo "<div>Download <a href='components/com_chronocontact/includes/gedeelte_h8/reports/{$report_name}' />hier</a>  de resultaten van uw eigen checklist en/of print het uit!</div>";
?>

Bob
teldrive 30 Aug, 2014
hi Bob, thanks by reply
will it be posible to dowload directly form web page without storing any file on host , because
it seems i have issues with permisions on my web hosting, alway says "can't open file'

<?php
 ob_start();
 ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
         <meta http-equiv='Content-type' content='text/html; charset=utf-8' />
     <title>Joe title</title>
         <style type='css/text'>. . .</style>

 <?php
 $content = ob_get_contents();
 ob_end_clean();
 $fp = fopen('/jobs/prueba.doc', 'w') or die("can't open file");
 fwrite($fp, $content);
 fclose($fp);
 echo "<div>Download <a href='/jobs/prueba.doc'>test</a> prueba</div>";
 ?>
GreyHead 30 Aug, 2014
Hi teldrive,

I think so. Here's another chunk of code from my CSV Export [GH] action for CFv5
    if ( $download_export || $download_nosave ) {
      // if Immediate download is checked
      \jimport( 'joomla.environment.browser' );
      $browser = \JBrowser::getInstance();

      switch ( $browser->getBrowser() ) {
      case 'msie':
        $inline = 'inline';
        $pragma = 'public';
        break;
      default:
        $inline = 'attachment';
        $pragma = 'no-cache';
        break;
      }
      $mimetype = 'text/csv';
      if ( $download_mime_type ) {
        switch ( $browser->getBrowser() ) {
          case 'msie':
            $mimetype = 'application/octetstream';
          case 'opera':
            $mimetype = 'application/octetstream';
            break;
          default:
            $mimetype = 'application/octet-stream';
            break;
        }
      }
      @ob_end_clean();
      ob_start();
      header( "Content-Type: {$mimetype}; charset=UTF-8" );
      header( 'Expires: '.gmdate( 'D, d M Y H:i:s' ).' GMT' );
      header( "Content-Disposition: {$inline}; filename={$file_name}" );
      header( "Content-Length: ".$filesize );
      if ( $browser->getBrowser() == 'msie' ) {
        header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
      }
      header( "Pragma: {$pragma}" );
      ob_clean();
      flush();
      if ( $download_nosave ) {
        print( $output );
      } else {
        readfile( $save_path.$file_name );
      }
      exit();
    }
  }

Bob
teldrive 31 Aug, 2014
Answer
Hi Bob,thanks 😀
i think it was solved with some clues you have given me, here is the working code.


<?php
  header("Content-type: application/vnd.ms-word");
  header("Content-Disposition: attachment; Filename=WordDoc.doc");
  ob_start();
  ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
          <meta http-equiv='Content-type' content='text/html; charset=utf-8' />
      <title></title>
          <body>

<a>my html data</a>


  </body>

  <?php
  $content = ob_get_contents();
  ob_end_clean();
  echo $content;
  ?>
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger