2017-02-03 4 views
-3

Code in index.phpHTML docx-Datei mit php

<?php 

$html = file_get_contents($_REQUEST['fname']); 
$filename = 'form_' . uniqid() . '.doc'; 
header("Content-type: application/vnd.ms-word"); 
header("Content-Disposition: attachment; filename= . $filename"); 
echo $html; 

?> 

Pfad der HTML-Datei

form_588f71c4e978d.html 

nach

http://localhost/html2/html2wordf.php?fname=form_588f71c4e978d.html 

im laufenden Code unten bekommen

<!doctype html> 
<html> 
<head> 
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8"> 
    <title></title> 
</head> 

<body> 
<p>hello</p> 
</body> 
</html> 

innen

form_588f71c4e978d.doc 

, aber ich möchte nur hello in dieser Datei

bedeutet html Konvertierung doc Daten nicht funktioniert, ist es nur von .html .doc seine Erweiterung zu ändern und Daten innerhalb sowohl in der Datei gleich bleiben

jemand meinen Punkt zu bekommen? Führe mich dank

+2

Wer hat Ihnen gesagt, dass * Snippet * würde HTML in eine richtige Word-Datei konvertieren? Es sendet lediglich einen gefälschten Content-Type. (Es sei denn es andere Code haben Sie nicht gezeigt.) – mario

+0

* bedeutet html doc Datenkonvertierung nicht * funktioniert natürlich ... es gibt keine Konvertierung – donald123

+0

HTML-Markup in einer Datei mit dem Namen ist weder 'doc', noch' docx '; es ist einfach HTML-Markup in einer Datei mit einer anderen Erweiterung –

Antwort

1

Es gibt gute PHP-Bibliothek, aber es ist nicht frei: http://www.phpdocx.com/documentation/introduction/html-to-word-PHP

Wenn Sie nur ersetzen möchten einen Text in der Textvorlage (Ich schlage vor, dass Sie docx-Format verwenden) Sie docx-Datei entpacken kann und Sie finden XML-Datei diesen Inhalt.

Sie können also str_replace ('{{youVariableInWordTemplate}}', $ value, $ wordXML);

Eine andere Möglichkeit: Verwenden Sie PhpWord

$phpWord = new PhpWord(); 
$section = $phpWord->addSection(); 
$html = '<p><strong>You html here</strong></p>'; 
Html::addHtml($section, $html); 

$objWriter = IOFactory::createWriter($phpWord, 'Word2007'); 
$cacheDir = '/temp_directory_of_your_project/'; 
$objWriter->save($cacheDir. 'helloWorld.docx'); 

Aber diese Bibliothek hat Problem mit Tabellenerzeugungs. Es ist Ausgabe geöffnet: Using table tag in HTML Reader produces no output mit Lösung benutzerdefinierten Klasse zu verwenden (siehe Anhang in der post)

Sie können auch eine verbesserte Implementierung finden: HTML Reader from PHPWord does't work with tables?

Diese Bibliothek Unterstützung nicht viele HTML-Tags (Array mit unterstützten Tags unten):

$nodes = array(
     // $method  $node $element $styles  $data $argument1  $argument2 
     'p'   => array('Paragraph', $node, $element, $styles, null, null,   null), 
     'h1'  => array('Heading',  null, $element, $styles, null, 'Heading1',  null), 
     'h2'  => array('Heading',  null, $element, $styles, null, 'Heading2',  null), 
     'h3'  => array('Heading',  null, $element, $styles, null, 'Heading3',  null), 
     'h4'  => array('Heading',  null, $element, $styles, null, 'Heading4',  null), 
     'h5'  => array('Heading',  null, $element, $styles, null, 'Heading5',  null), 
     'h6'  => array('Heading',  null, $element, $styles, null, 'Heading6',  null), 
     '#text'  => array('Text',  $node, $element, $styles, null, null,   null), 
     'span'  => array('Span',  $node, null,  $styles, null, null,   null), //to catch inline span style changes 
     'strong' => array('Property', null, null,  $styles, null, 'bold',   true), 
     'em'  => array('Property', null, null,  $styles, null, 'italic',  true), 
     'sup'  => array('Property', null, null,  $styles, null, 'superScript', true), 
     'sub'  => array('Property', null, null,  $styles, null, 'subScript', true), 
     'table'  => array('Table',  $node, $element, $styles, null, 'addTable',  true), 
     'tbody'  => array('Table',  $node, $element, $styles, null, 'skipTbody', true), //added to catch tbody in html. 
     'tr'  => array('Table',  $node, $element, $styles, null, 'addRow',  true), 
     'td'  => array('Table',  $node, $element, $styles, null, 'addCell',  true), 
     'ul'  => array('List',  null, null,  $styles, $data, 3,    null), 
     'ol'  => array('List',  null, null,  $styles, $data, 7,    null), 
     'li'  => array('ListItem', $node, $element, $styles, $data, null,   null), 
    );