1

i have a problem. I create application JavaFX management for data base. I decided to convert data from tableview to pdf. For this I used a pdfbox. I can't change font in my converter pdf. How can do it? Because helvetica does not support the UTF-8 (ą, ę, ź ...).

enter code here public enum Orientation{
    PORTRAIT, LANDSCAPE
};

public boolean doPrintToPdf(List<List> list, File saveLoc,Orientation orientation) {
    try {
        if (saveLoc == null) {
            return false;
        }
        if (!saveLoc.getName().endsWith(".pdf")) {
            saveLoc = new File(saveLoc.getAbsoluteFile() + ".pdf");
        }
        //Inicjalizacja dokumentu
        PDDocument doc = new PDDocument();
        PDPage page = new PDPage();

        if (orientation == Orientation.LANDSCAPE) {
            page.setMediaBox(new PDRectangle(PDRectangle.A4.getHeight(), PDRectangle.A4.getWidth()));
        } else {
            page.setMediaBox(new PDRectangle(PDRectangle.A4.getWidth(), PDRectangle.A4.getHeight()));
        }

        doc.addPage(page);
        float margin = 10;
        float tableWidth = page.getMediaBox().getWidth() - (2 * margin);
        float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
        float yStart = yStartNewPage;
        float bottomMargin = 0;

        BaseTable dataTable = new BaseTable(yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, true,
                true);
        DataTable t = new DataTable(dataTable, page);
        t.addListToTable(list, DataTable.HASHEADER);
        dataTable.draw();
        doc.save(saveLoc);
        doc.close();

        return true;
    } catch (IOException e) {
        DialogsUtils.errorDialog(e.getMessage());
    }
    return false;
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
Krzys421
  • 11
  • 2
  • Possible duplicate of [U+0151 ('odblacute') is not available in this font Times-Roman encoding: WinAnsiEncoding](https://stackoverflow.com/questions/46351353/u0151-odblacute-is-not-available-in-this-font-times-roman-encoding-winansi) – Tilman Hausherr Oct 24 '18 at 10:22
  • I do not use stream, because creates a table by itself – Krzys421 Oct 24 '18 at 10:43
  • You can also use a file, as explained in the answer: `PDFont font = PDType0Font.load(document, new File("c:/windows/fonts/font.ttf"));` Pass that font to `content.setFont()`. – Tilman Hausherr Oct 24 '18 at 10:45
  • I've tried this before, but the automatic creation of the table bypasses the stream – Krzys421 Oct 24 '18 at 10:54
  • I see what you mean... have a look at https://stackoverflow.com/questions/28059563/how-to-create-table-using-apache-pdfbox , this one uses `TableBuilder` which allows to set fonts. However I know nothing about boxable, so I don't know if this will help you. – Tilman Hausherr Oct 24 '18 at 10:58
  • I had a look at the boxable repository... a lot of classes do accept fonts, but not DataTable. The best would be that you grab the project and make the change yourself. – Tilman Hausherr Oct 24 '18 at 11:09
  • @Krzys421 can you please add all the things you already tried in your answer, so people won't be wasting time suggesting solutions you already know don't work? – Mike 'Pomax' Kamermans Oct 25 '18 at 15:11
  • @Mike'Pomax'Kamermans you meant to write "question", I think. (I mention this not to correct you, but to prevent a beginner from writing an answer that would be deleted thus increasing frustration) – Tilman Hausherr Oct 29 '18 at 08:42
  • Unfortunately, nothing worked. I used a different library and create tables manually. Everything works. – Krzys421 Oct 29 '18 at 17:04
  • @Krzys421 then please write an answer that explains to use that library, and how to manually create the tables, so that future visitors with the same problem may have a way forward, too. – Mike 'Pomax' Kamermans Oct 30 '18 at 00:43
  • I had another look... try `DataTable.getDataCellTemplateEven().setFont()` and the other getters of `DataTable`. Btw your code is confusing, `t` is a `DataTable`, and `dataTable` is not a `DataTable`but a `BaseTable`. – Tilman Hausherr Oct 30 '18 at 10:38
  • @Mike 'Pomax' Kamermans I'm sorry but I can not answer because I have too few reputation points – Krzys421 Oct 30 '18 at 11:54
  • As per https://stackoverflow.com/help/privileges/create-posts: yes you do. SO wouldn't work all that well if you needed more than 1 reputation to even ask questions or post answers. – Mike 'Pomax' Kamermans Oct 30 '18 at 20:33

0 Answers0