Auto page break in libHaru PDF

974 Views Asked by At

I'd like to add an automatic page break to a libHaru PDF in iOS.

I do have several text fields in the app which contain the user filled data. when i generate the pdf i first measure the expected size of the text-rect going to be created. if it exceeds the remaining space i trigger a hpdf_new_page event and put the text on an new page. i'd like to have this just in part automatically. so if the text exceeds the space on the current page it should split and continue on a new page without me checking or doing anything. unfortunately i can't find anything like this in the documentation.

2

There are 2 best solutions below

1
On

Line counting using fgets() may help. When your print program opens a file to print, each line can be copied to the pdf file and checked for a form feed character

or

if the line count has reached a limit.

Another possible solution is to use a character count limit with "while(getc(file) != EOF)".

This link uses libharu to print basic text files with PCL commands to change the font. https://github.com/DaDaDadeo/GetCycle/blob/master/pcl_to_pdf.c

The form feed character '\f' (ascii 12) and 61 lines will trigger a new page. There are other conditions in the program to restrict a new page but the general idea is illustrated.

The results are the same as a printer using telnet raw 9100 protocol. The pcl commands are limited to just a couple of font changes so it is not too complicated.

0
On

Libharu is rather low-level library, and I could not even expect of appearing such automatic page splitting in newer versions due to number of reasons. Hereafter I state two of them:

  1. There is no good, preferred strategy how to place remaining of non-fitting text on the next page. In some cases it could be even impossible at all.
  2. There is no good, preferred strategy for text splitting.

Why?

  1. Consider your font is extremely large, and just one letter (for instance, wide one as "W") does not fit into the page. Where we are supposed to place it? On the next page? Ok, we add new page... oops, it does not fit this page too - as soon as all our pages have the same size. Dead-end without any good, straightforward way out. In other words, there should be a user-defined strategy for these cases. Almosy every naive implementation will have such a corner cases.

  2. libharu does not know where it should split your text automatically. It does not know hyphenation rules of your language, it does not know whether it should respect spaces or not (wrap whole words only or not), and so on. It's up to you to specify these rules.

So, you should call HPDF_Font_MeasureText for some part of your text string, decide if it fits into your page (excluding margins, footers - which also out of libharu's internal knowledge) and render it. And note that there is no simple formula for text size depending on its length. String "wwww" is more than twice wider than "iiii", of course if your font is not mono-spaced.