Html2pdf doesn't support word-break:break-all css
Asked Answered
T

10

9

hai everybody i am using html2pdf ,it doesn't support word-break:break-all css any idea?

example

   <td style="width:30%;word-break:break-all ;">
      testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets
    </td>

output pdf take above 30% width like string length size

output pdf: testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets

I want Output :

testtestetstetstetstetstettstets
tetstetstetstetstetstetstetstets

Tragicomedy answered 6/10, 2012 at 11:50 Comment(6)
why dont you count the characters ?Indefectible
Have you tried with actual content? It might work better for you instead of fixing something that's not likely to happen in production.Assoil
Due to rendering problems with html2pdf, we used wkhtmltopdf as it uses the webkit rendering engine. With this, we created PDF reports upwards of 25pp.Deluca
wkhtml also supports some kinda page break thing, you can keep stuff on the same page with page-break-inside: avoid;Borchert
@Tragicomedy Migrating to wkhtmltopdf or phantomjs was probably the best idea anyway ;)Goodrich
I fixed it by looping through the characters in the string and manually putting in a line break and a - (if mid word) where neededNeoteny
T
2

html2pdf does not support this word-break:break-all css

Ref: http://www.yaronet.com/en/posts.php?sl=&h=0&s=151321#0

Tragicomedy answered 23/10, 2012 at 12:20 Comment(0)
G
15

Well, that's complicated. Your teststring is too long, but it's not composed of multiple words. That means that word-break won't work, because there aren't any words to break on. Obviously, this might well just be an example, in which case it might be that html2pdf just doesn't support relative widths and word-break, so you could try having an absolute width and word-break.

That said, here's something I know that will work: wordwrap in PHP. So, instead of echo $yourvar; you could use echo wordwrap($yourvar, 75, "\n", true) instead, which will always cut the string, even if it's just one long string. It takes a little fiddling to get the number of characters to match up with the width that you're looking for, but it will work.

<?php
$foo = str_repeat('test',12);
echo wordwrap($foo, 20, '<br />', true);

Output:

testtesttesttesttest
testtesttesttesttest
testtest
Goodrich answered 8/10, 2012 at 12:24 Comment(3)
it is all dynamic content like user input firstname lastnameTragicomedy
@Tragicomedy So? You can still use wordwrap to accomplish your goal.Goodrich
Actually, using wordwrap is the only way for HTML2PDF these days... Pity I didn't use mPDF instead... One caveat though - the line braker has to be HTML's <br />: wordwrap($string, $length, '<br />');.Larrikin
D
7

try this;

<td style="width:30%; word-wrap:break-word;">
   testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets
</td>

not word-break it is word-wrap ;

Donata answered 14/10, 2012 at 20:4 Comment(5)
impossible it.I think there isnt a table tag of your html page.you can set td instead of div .Donata
i know it is work only html pages not in html2pdf thats why i am asking question hereTragicomedy
html2pdf does not support this yaronet.com/en/posts.php?sl=&h=0&s=151321#0Tragicomedy
@ErhanSönmez - what makes you think this is "impossible"? CSS support is all over the place across platforms... it is not at all unusual that file-type conversion systems trip over rare-er css rules (or even non-rare ones, like float). The question pertains to a constructed PDF, not an HTML page rendered in a browser.Garnes
Nice one, that did it for me. Thx!Hakeem
G
5

If you want long strings to wrap consistently within a boundary container I think you should be able to accomplish this by inserting zero-width space characters (&#8203; or \xe2\x80\x8b) between every letter of the orignial string. This will have the effect of wrapping as if every character was its own word, but without displaying the spaces to the end user. This may cause you trouble with text searches or indexing on the final product, but it should accomplish the task reliably from an aesthetic perspective.

Thus:

testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets

Becomes

t&#8203;e&#8203;s&#8203;t&#8203;t&#8203;e&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s&#8203;t&#8203;e&#8203;t&#8203;s

(which displays: "t​e​s​t​t​e​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s​t​e​t​s")

So if you wrap it it will wrap exactly to the bounds of its container. Here's a fiddle of it as an example.

Just write a PHP script to loop though the string and insert the space:

$string="testtestetstetstetstetstettstetstetstetstetstetstetstetstetstetstets";
$new_string = "";
for($i=0;$i<strlen($string);$i++){
   if ($string[$i]==' ' || $string[$i+1]==' '){ //if it is a space or the next letter is a space, there's no reason to add a break character
      continue;
   }
   $new_string .= $string[$i]."&#8203;";
}
echo $new_string

This is a particularly nice solution, because unlike wordwrap(), it automatically adjusts for non-fixed-width fonts (which is basically 99% of fonts that are actually used).

Again, if you need to resulting PDF to be searchable, this is not a good approach, but it will make it look like you want it to.

Garnes answered 11/10, 2012 at 22:37 Comment(5)
What encoding are you using? And are the ? only showing up in the PDF or both the PDF and the HTML file?Garnes
See what happens if you use \xe2\x80\x8b instead of &#8203;Garnes
I've added the UTF-8 code in the answer as well, so if you're using that encoding just change the $new_string .= $string[$i]."&#8203;" line to $new_string .= $string[$i]."\xe2\x80\x8b"Garnes
instead of inserting a zero-width space character, you can insert an empty HTML tag e.g. <i></i>, this way the text will continue searcheableParishioner
I get same problem as Srini, force Arial not working too : <span style=\"font-family: Arial!important;\">&#8203;</span> Same for \xe2\x80\x8b <i></i> not workingStrongroom
O
4

In your testing the word break will not work because the word break only works between the words in a particular sentence. So yo can use the multiple word sentence and then try with the word breaker

Obryant answered 12/10, 2012 at 6:17 Comment(0)
R
4

You just use substr function in your code. I put a example for this. First put your output in variable.

$get_value = "testtestetstetstetstetstettstetstetstet";
$first = substr("$get_value",0,3);
$second = substr("$get_value",4,7);

and so on.

Rivard answered 12/10, 2012 at 7:8 Comment(0)
G
3

You can use "\r\n" to print newline character. make sure to use it with double quote. If your string is in the variable then you need to use word count function and append this string. You can also use PHP_EOL to avoid platform dependency.

Gourmet answered 8/10, 2012 at 12:55 Comment(0)
T
2

html2pdf does not support this word-break:break-all css

Ref: http://www.yaronet.com/en/posts.php?sl=&h=0&s=151321#0

Tragicomedy answered 23/10, 2012 at 12:20 Comment(0)
L
1

I want to add little bit of own experience with HTML2PDF and tables.

I used this solution to generate the PDF containing a table filled with delivery confirmation (list of products). Such list may contain up to thousand of products (rows).

I encountered a problem with formatting and long strings in cells. First problem was that the table was getting too wide even if I set the table's width to 100% and the width of header (<th>) columns (HTML2PDF does not support <colgroup> so I couldn't define it globally) - some columns were out of visible area. I used wordwrap() with <br /> as separator to break down the long strings which looked like it's working. Unfortunately, it turned out that if there is such long string in first and last row the whole table is prepended and appended with empty page. Not a real bugger but doesn't look nice either. The final solution was to (applies for tables which width could outreach the visible area):

  • set the fixed widths of table and each row in pixels
    • for A4 letter size I am using total width of 550 px with default margins but you'd have to play around a little to distribute the width between columns
  • in wordwrap use empty space or &#8203; / \xe2\x80\x8b as delimiter

For small tables that you'd like to spread for 100% of visible area width it is OK to use width expressed in %.

Larrikin answered 12/8, 2015 at 7:21 Comment(0)
E
0

You may use this method.

<?php
$get_value = "testtestetstetstetstetstettstetstetstet";
$first = substr("$get_value",0,3);
$second = substr("$get_value",4,7);
$third = substr("$get_value",8,11);
?>
Eads answered 22/10, 2012 at 7:44 Comment(0)
A
-1

I think this function is a limping solution.

function String2PDFString($string,$Length)
    {
    $Arry=explode(" ",$string);
    foreach($Arry as $Line)
        {
        if(strlen($Line)>$Length)
            $NewString.=wordwrap ($Line,$Length," ",true);
        else
            $NewString.=" ".$Line;
        }
    return $NewString;
    }
Aindrea answered 24/11, 2014 at 21:0 Comment(1)
Killing an ant with a hammer. A bad hammer.Larrikin

© 2022 - 2024 — McMap. All rights reserved.