Reputation: 23
I'm using Laravel with phpword. So here i want to export html layout from blade file to word.
But when i trying to create it, the error that appearing :
DOMDocument::loadXML(): Opening and ending tag mismatch: link line 1 and head in Entity, line: 1
My code:
$content = view('docs.index')->render();
$dom = new DOMDocument();
$dom->loadHTML($content);
$dom->saveHTML();
$phpWord = new PhpWord();
Html::addHtml($phpWord->addSection(), $dom->saveHTML(), true);
$objWriter = IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('doc_index_'.Carbon::now()->format('d-m-y h-i').'.docx');
return view('papers.show')->with('success');
Here is my blade file :
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doc HTML</title>
<meta charset="utf-8">
<link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css" />
</head>
<body>
<div>
<p>
Laravel is a web application framework with expressive, elegant syntax. <br />
We believe development must be an enjoyable and creative experience to be truly fulfilling. <br />
Laravel takes the pain out of development by easing common tasks used in many web projects. <br />
</p>
</div>
</body>
</html>
Printing my $content variable :
"""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Doc HTML</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css" />
</head>
<body>
<div>
<p>
Laravel is a web application framework with expressive, elegant syntax. <br />
We believe development must be an enjoyable and creative experience to be truly fulfilling. <br />
Laravel takes the pain out of development by easing common tasks used in many web projects. <br />
</p>
</div>
</body>
</html>
"""
Upvotes: 1
Views: 2693
Reputation: 101
I was facing exact same issue and got fixed with below work arou
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=".rand().".doc");
header("Pragma: no-cache"); header("Expires: 0");
and then simply render your view. E.g return view('view_name');
Upvotes: 1
Reputation: 2710
The <!DOCTYPE html>
part in the beginning of your template is causing that error. Regarding reading the style files, from the link to the function details provided there above in discussion PHPWord/Html.php you can see that addHtml will just use the contents of the body tag and ignore everything else.
Regarding the other question in the above discussion, I don't think that PHPWord supports any kind of style inclusion from css files - in this particular example, I'm not sure what would be the expected result in applying bootstrap responsive styling to word document content.
Upvotes: 0
Reputation: 6291
Your <meta>
tag is technically not closed. To follow the spec for elements that ommit a closing tag, they must be suffixed with />
. So just how you have <br />
and <link ... />
your meta tags should be <meta ... />
.
Upvotes: 0