-
-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tables row are not aligned when using external fonts #32
Comments
Can you please try this type of loading when you are working with TrueType font PDFont tnrFont = PDTrueTypeFont.loadTTF(document, "c://temp//font//timesbd.ttf"); Here is my example with other external font |
there is still an issue: On Tue, Mar 15, 2016 at 1:27 PM, Hrvoje Štimac notifications@github.com
|
any direction where is the issue? how to fix? |
I will look this more thoroughly on the weekend but here is good starting point (in the comment section) : https://issues.apache.org/jira/browse/PDFBOX-3138 Also I caught random comment which I saved for later
|
Thanks. Was not able to solve it. So if you can provide sample, it would help. Thanks a lot. From: Hrvoje Štimac [mailto:notifications@github.com] I will look more thoroughly on the weekend but here is good starting point (in the comment section) : https://issues.apache.org/jira/browse/PDFBOX-3138 — |
Unicode output in PDF is royal pain. Sadly I don't have time to make this work but here is the most accurate thread that I could find : http://stackoverflow.com/questions/1713751/using-java-pdfbox-library-to-write-russian-pdf. |
Thanks a lot, On Mon, Mar 21, 2016 at 12:49 PM, Hrvoje Štimac notifications@github.com
|
I took a stab at this, and I think it's possibly just an issue with your particular font;
The output looks ok to me (but then, I don't speak any Russian). |
Can you please add this line in some of the test cases and post here PDF output? I really want to see if formating is broken. |
example: code related to it: font used are win7 and are the official one provided by win. if you will send me your ttf file can try to test it as well. |
another sample with ariel.ttf |
Please do attach the font files you are using. |
Just rename it (remove the .txt) |
Thanks, will review as soon as I have a chance, most likely not today. |
Thanks. good luck. |
@dluzzon I have an unrelated question, but I don't think there's a good way on github to contact people. I see you are also using XChart, and I was wondering if you are using that inside pdf's using pdfbox as well? If so, would you mind sharing some examples with me, I think this could be really usefull for some of the projects I am working on. |
Sure, will send you tomorrow. Not near the desc most of the day today. From: Dries Horions [mailto:notifications@github.com] @dluzzon https://github.com/dluzzon I have an unrelated question, but I don't think there's a good way on github to contact people. I see you are also using XChart https://github.com/dluzzon/XChart , and I was wondering if you are using that inside pdf's using pdfbox as well? If so, would you mind sharing some examples with me, I think this could be really usefull for some of the projects I am working on. Feel free to email me : — |
I haven't been able to resolve the issue yet, but I think I did find the cause for it. For those externally loaded fonts, this doesn't seem to return correct values. File tnrFontFile = new File("c://temp//font//times.ttf");
PDType0Font tnrFont = PDType0Font.load(doc, tnrFontFile);
File tnrBoldFontFile = new File("c://temp//font//timesbd.ttf");
PDType0Font tnrBoldFont = PDType0Font.load(doc, tnrBoldFontFile);
//The problem is FontUtils calculating the height incorrectly
float tnrFontHeight = FontUtils.getHeight(tnrFont, 14);
float tnrFontDescent = FontUtils.getDescent(tnrFont, 14);
float defaultFontHeight = FontUtils.getHeight(PDType1Font.HELVETICA_BOLD, 14);
float defaultDescent = FontUtils.getDescent(PDType1Font.HELVETICA_BOLD, 14);
System.out.println(
tnrFontFile + " height : " + tnrFontHeight + "\n"
+ tnrFontFile + " descent : " + tnrFontDescent + "\n"
+ "PDType1Font.HELVETICA_BOLD" + " height : " + defaultFontHeight + "\n"
+ "PDType1Font.HELVETICA_BOLD" + " height : " + defaultDescent + "\n"
); This results in :
I'll try to figure out what can be done about it, but if anyone else has ideas, feel free to comment. |
It seems PDFont.getHeight is deprecated https://pdfbox.apache.org/docs/2.0.0/javadocs/org/apache/pdfbox/pdmodel/font/PDFont.html#getHeight(int) I did a small test modifying FontUtils.createFontMetrics like this : private static void createFontMetrics(final PDFont font) throws IOException
{
//Handle type0 fonts differently
if (font instanceof PDType0Font)
{
final float height = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * 0.865f;
fontMetrics.put(font.getName(), new FontMetrics(height, 0, 0));
}
else
{
final float base = font.getHeight("a".codePointAt(0)) / 1000;
final float ascent = font.getHeight("d".codePointAt(0)) / 1000 - base;
final float descent = font.getHeight("g".codePointAt(0)) / 1000 - base;
fontMetrics.put(font.getName(), new FontMetrics(base + ascent + descent, ascent, -descent));
}
} That seems to deliver better results, but isn't a good final solution. what do you think would be the best way to determine the height of the text? |
Hi, just a thought. Why do you need to calculate the height of the text? Isn’t the row height is an input to the row on create row? From: Dries Horions [mailto:notifications@github.com] I did a small test modifying FontUtils.createFontMetrics like this : private static void createFontMetrics(final PDFont font) throws IOException That seems to deliver better results, but isn't a good final solution. @mkuehne https://github.com/mkuehne @Frulenzo https://github.com/Frulenzo what do you think would be the best way to determine the height of the text? BoxableSample1.pdf https://github.com/dhorions/boxable/files/189208/BoxableSample1.pdf — |
Also running in font alignment issues, most of them are solved by using the correct font loader. However, Cell.setHeaderCell seems to also break alignment. |
Hi, we face several problems here. First of all, working with hebrew (or Unicode) characters works fine for me using the provided Times font. Type 1 fonts only support 8-bit code points, as pointed out by the thrown exception: java.lang.IllegalArgumentException: Can't encode U+05DC in font Helvetica. Type 1 fonts only support 8-bit code points
at org.apache.pdfbox.pdmodel.font.PDType1Font.encode(PDType1Font.java:343)
at org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:283)
at org.apache.pdfbox.pdmodel.font.PDFont.getStringWidth(PDFont.java:312)
at be.quodlibet.boxable.Paragraph.getLines(Paragraph.java:83)
... My output looks like this: I also stumbled over the calculation you propose, but I did not like the magic number Fetching the font's height via the bounding box does not provide data about the ascent and descent, but maybe we can avoid using them. But evaluating the necessary changes will need some time. Using your font height fix and the provided Times font a single cell looks like this: @dhonorez Could you please provide an example? |
@dobluth I solved it by instead of using the Cell.setHeaderCell, using the table.setHeader() method. This does not work, as in, does not align correctly with external font:
This works:
PS: this was on version 1.2.2 , version 1.3 is slightly more consistent. |
Hi,
few problems I still have:
here is my code:
|
Check PR #37 . We are using fontDescriptor methods for retrieving font metrics, avoiding deprecated methods and magic numbers ;) Now the cell content looks like this: @dluzzon |
@Frulenzo That looks promising, I'll try to find time tomorrow to review and merge. |
works perfect. |
@dluzzon My example [...]
//Create Header row
Row<PDPage> row = table.createRow(15f);
Cell<PDPage> cell = row.createCell((100 / 3f), "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tincidunt consequat tincidunt. Donec gravida, justo tristique volutpat aliquet, mauris nulla pulvinar libero, vel commodo magna ex ac dolor. Integer feugiat velit a leo commodo viverra. Integer volutpat magna ac blandit luctus. Etiam lobortis nulla a placerat consequat. Aliquam orci orci, pretium eu quam eu, malesuada hendrerit ipsum. Quisque imperdiet mi in dui lacinia tincidunt. Integer id varius ligula, et aliquet elit. Phasellus facilisis et urna ut scelerisque. Nunc et neque nec arcu malesuada tristique fermentum ut ante. Donec accumsan in urna id fringilla. Mauris in tempor tellus. Mauris in semper purus, auctor dapibus risus. Quisque maximus lorem vel sem suscipit faucibus. Fusce velit diam, tincidunt ac est at, feugiat dapibus diam. Ut malesuada, arcu fringilla pretium placerat, felis velit elementum ante, sed fermentum eros enim a arcu. ", HorizontalAlignment.get("center"),
VerticalAlignment.get("top"));
cell.setFont(PDType1Font.HELVETICA);
cell.setFontSize(6);
Cell<PDPage> cell2 = row.createCell((100 / 3f), "It's me", HorizontalAlignment.get("center"),
VerticalAlignment.get("middle"));
cell2.setFont(PDType1Font.HELVETICA);
cell2.setFontSize(6);
Cell<PDPage> cell3 = row.createCell((100 / 3f), "I was wondering", HorizontalAlignment.get("center"),
VerticalAlignment.get("bottom"));
cell3.setFont(PDType1Font.HELVETICA);
cell3.setFontSize(6);
Row<PDPage> row2 = table.createRow(15f);
Cell<PDPage> cell4 = row2.createCell((100 / 3.0f), "Hello", HorizontalAlignment.get("center"),
VerticalAlignment.get("top"));
cell4.setFont(PDType1Font.HELVETICA);
cell4.setFontSize(6);
Cell<PDPage> cell5 = row2.createCell((100 / 3f), "can you hear me?", HorizontalAlignment.get("center"),
VerticalAlignment.get("middle"));
cell5.setFont(PDType1Font.HELVETICA);
cell5.setFontSize(6);
Cell<PDPage> cell6 = row2.createCell((100 / 3f),
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce tincidunt consequat tincidunt. Donec gravida, justo tristique volutpat aliquet, mauris nulla pulvinar libero, vel commodo magna ex ac dolor. Integer feugiat velit a leo commodo viverra. Integer volutpat magna ac blandit luctus. Etiam lobortis nulla a placerat consequat. Aliquam orci orci, pretium eu quam eu, malesuada hendrerit ipsum. Quisque imperdiet mi in dui lacinia tincidunt. Integer id varius ligula, et aliquet elit. Phasellus facilisis et urna ut scelerisque. Nunc et neque nec arcu malesuada tristique fermentum ut ante. Donec accumsan in urna id fringilla. Mauris in tempor tellus. Mauris in semper purus, auctor dapibus risus. Quisque maximus lorem vel sem suscipit faucibus. Fusce velit diam, tincidunt ac est at, feugiat dapibus diam. Ut malesuada, arcu fringilla pretium placerat, felis velit elementum ante, sed fermentum eros enim a arcu. ",
HorizontalAlignment.get("center"), VerticalAlignment.get("bottom"));
cell6.setFont(PDType1Font.HELVETICA);
cell6.setFontSize(6);
table.draw();
[...] |
Yes, all fine now. |
On Sample 1 I have added fonts:
` //fonts to be used by the doc
`
in the for loop used the new Fonts with size 14:
the output was this problematic pdf:
BoxableSample1_with_PDType0Font.pdf
there are few problems here:
The text was updated successfully, but these errors were encountered: