0

My PHP generates a Word document, but it will not render carriage returns. My CKEditor translates a carriage return into either,

<br>, or <div>asdf</div>

When the Word document is created, it will display those HTML tags, so I strip them out. What replacement code, character, ascii, or tag can I use so that when the page is rendered, it shows the text like it did in the Editor?

Current example - if you have the text "Don't jump off the" [then hit Enter, so that the next word is below it]...

"cliff." Instead, currently, that gets saved into SQL as:

Don't jump off the <br>cliff.
Don't jump off the <div>cliff</div>.

...depending on which browser is used. In the msWord output, any tags left in the content [exceptions to strip_tags function] get displayed literally in msWord. Or, if I replace the tags with ASCII it displays that literally, too. Not sure if this helps, but this is defined at the top of my php report_generator.php file:

require_once '/var/www/PhpWord/src/PhpWord/Autoloader.php';
\PhpOffice\PhpWord\Autoloader::register();
include "/var/www/ncpcphp/NCPC_PHP_Functions.php";
DEFINE("WRITEtoFDOCS", "NO");
DEFINE("FDOCSDIRECTORY", "Contract Attachments");
DEFINE("MIMETYPE","application/vnd.openxmlformats-officedocument.wordprocessingml.document" );

Help - what can I use to cause the output to show the carriage return?

dcparham
  • 291
  • 4
  • 17
  • How are you adding the html to your word document ? – troosan Mar 10 '18 at 06:59
  • 1
    I don't know php but Word uses the ANSI character 13 for a new paragraph. In VB-languages I use Chr(13) as part of a string value. In C# \n or \r works. If php is like C# then: `Don't jump off the\ncliff` – Cindy Meister Mar 10 '18 at 11:10
  • 1
    Possible duplicate of [PHP - how to create a newline character?](https://stackoverflow.com/questions/4238433/php-how-to-create-a-newline-character) – Cindy Meister Mar 11 '18 at 19:38
  • 1
    You'll find the information I gave you confirmed in https://stackoverflow.com/questions/4238433/php-how-to-create-a-newline-character – Cindy Meister Mar 11 '18 at 19:38

1 Answers1

0

Thank you, Cindy. Your answer is a piece of the solution. My CKEditor saves carriage returns as either

<br>, <br />, or <div></div>

depending on which browser is used Chrome[div] or Firefox[br] - that goes into my msSQL. Yet, if my editor has turned-on an "Enter filter" [keycode-13] in order to prevent someone from enter-editing a [[Placeholder]] (ckeditor read-onlyplugin) then saved editor text strips out the Enter. So, I did this: kept in the enter filter because it is necessary, then after the text carriage returns are saved as br's or div's, when I run my report generator, I replace

<br>, <br />, and <div></div> 

with "\n" like you said. Then I explode the text variable like this:

$show_cad = explode("\n", $show_cad);
    foreach($show_cad as $line) {
       $section->addText(htmlspecialchars($line));
    }

The code in the editor needed to filter out the Enters only when Enter is pressed in the [[Placeholder]] plug in is more complex, and I got largely from CKEditor themselves. They request that I do not post their full solutions, but if you like I could show you pieces of it. Basically, I had to register the Placeholder widget. Then when Enter is pressed, it checks if the context is with Placeholder. If so, filter the Enter [then it disappears from the saved editor text], but if Enter is pressed elsewhere, it gets saved and used properly.

Thank you for your help!

dcparham
  • 291
  • 4
  • 17