Generating DOC from HTML content

Asked

Viewed 538 times

1

I’m using the plugin Phpword to convert HTML text into a . docx file

But when I go to download the generated file, I get only with very crazy characters like:

PK#############! #######[Content_types]. xml N 0#E |E -J @#5 #*Q>5' _ 8} = D#AC v#) s G G 5 "j J6,#,#' nq s~ 2L )a m# d|5 m#F #K L) s r V #8 T>Z 5.x# C,

I already searched here in stackoverflow, in the project’s own github and in google in general, but I could not solve the problem.

My code goes next:

<?php
error_reporting (E_ALL | E_STRICT);
@ini_set ('display_errors', 'on');
require_once '../../../../vendor/autoload.php';

$my_content = $_POST['html_content'];

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();

\PhpOffice\PhpWord\Shared\Html::addHtml($section, $my_content);

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachement;filename="teste.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('php://output');
?>
  • Check the encodes in the file generation, add the header pro correct format. An example would be to add header ('Content-type: text/html; charset=UTF-8');

  • Thank you for trying to help @Marcelodeandrade. I tried this way and several others in the header, nothing solved. I decided to save the file in a temporary folder and then download it. It worked, as soon as it’s 100% ready I’ll post the solution here.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.