5

I have a design template that forces me to use very large uppercase letters for a headline. If I have a long word that does not fit into one line, I run into expected problems because RL does not have built-in hyphenation – that’s okay. If I have a long compound word, however, that contains a hyphen (e.g. "VVVVEEEERRRRYYYY-LLLLOOOONNNNGGGG"), it is not broken at the hyphen as I would expect.

Expected:

|VVVVEEEERRRRYYYY-      |
|LLLLOOOONNNNGGGG       |

Actual:

|VVVVEEEERRRRYYYY-LLLLOO|
|OONNNNGGGG             |

How can I tell ReportLab to perform a conditional line-break when encountering a hyphen?

BTW, the PDF is generated using Python 3.4 and reportlab 3.1.8 like so:

doc = BaseDocTemplate(fname,
                      leftMargin=20 * mm, rightMargin=20 * mm,
                      topMargin=25 * mm, bottomMargin=20 * mm)
story = []
frame_first_page = Frame(doc.leftMargin, doc.bottomMargin, doc.width,
                         doc.height - 24 * mm,
                         leftPadding=0, rightPadding=0, id='first')
doc.addPageTemplates([PageTemplate(id='first_page',
                      frames=frame_first_page,
                      onPage=_on_first_page),
                      PageTemplate(id='remaining_pages',
                      frames=frame_remaining_pages,
                      onPage=_on_remaining_pages)])
story.append(Paragraph(heading_text.upper(), styles["title"]))
doc.build(story)
ilpssun
  • 348
  • 2
  • 7
  • Could you show the code where you print this information on your canvas? If I understand well, you will need to `split` the string yourself and print each part separately. – jbihan Jan 28 '15 at 15:26
  • @jbihan I updated my question with a code fragment showing how I add a paragraph with the title. – ilpssun Jan 28 '15 at 15:37
  • According to this link: http://stackoverflow.com/a/9424336/1725980, it seems that you could use `
    ` to add a line break. So you could try to add `heading_text.replace('-', '-
    ')` before appending the `Paragraph` to the doc... But honestly I didn't try it.
    – jbihan Jan 28 '15 at 18:26
  • @jbihan That would probably be a feasible (yet tedious) work-around. I'd much rather solve this generally for all hyphens in paragraphs. Also, I'd have to determine somehow whether a line-break after the hyphen is actually needed. – ilpssun Jan 29 '15 at 12:46

1 Answers1

1

This is an old question, but I stumbled upon the same problem today and found a solution: ReportLab seems to only wrap on white-space, so the idea is to add white-space with no visual space. Sadly it does not recognize the „zero width space“ or the „zero width non-joiner“ ‌ as a word-boundary, but the „thin space“ (encoded e.g. as  ) and the even thinner „hair space“   works and in my tests are good enough regarding no visual space.

So replacing all hyphens in your content should help: text_content.replace("-", "- ")

Richard
  • 11
  • 1