allcolor / YaHP-Converter

YaHP is a Java library that allows you to convert an HTML document into a PDF document.
GNU Lesser General Public License v2.1
56 stars 23 forks source link

Changing fonts not working #33

Closed sibidharan closed 7 years ago

sibidharan commented 9 years ago

I am using this code

        FileOutputStream out = null;
        try {
            CYaHPConverter converter = new CYaHPConverter();
            if (!filename.endsWith(".pdf")) {
                filename = filename + ".pdf";
            }
            File fout = new File(filename);
            out = new FileOutputStream(fout);
            Map properties = new HashMap();
            List headerFooterList = new ArrayList();
            headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter(
                    "<table width=\"100%\"><tbody><tr><td align=\"left\">"
                    + "</td><td align=\"right\">Page <pagenumber>/<"
                    + "pagecount></td></tr></tbody></table>",
                    IHtmlToPdfTransformer.CHeaderFooter.HEADER));
            headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter(
                    "<center>2015 © Jeevetha Decorators</center>",
                    IHtmlToPdfTransformer.CHeaderFooter.FOOTER));

            properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
                    IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
            properties.put(IHtmlToPdfTransformer.PDF_AUTHOR, "Jeevitha Decorators");
            properties.put(IHtmlToPdfTransformer.PDF_CREATOR, "Blovia Inc.");
            properties.put(IHtmlToPdfTransformer.PDF_TITLE, "Reference");
            properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, new File("calibri.ttf").getAbsolutePath());
            converter.convertToPdf(new URL(url),
                    IHtmlToPdfTransformer.A4P, headerFooterList, out,
                    properties);
            out.flush();
            out.close();
            return fout;
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IHtmlToPdfTransformer.CConvertException | IOException ex) {
            JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                out.close();
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;

I placed the ttf file in the same folder. But the generated pdf is not embedding and displaying the font. I have also specified

*{
    font-family:Calibri;
    font:Calibri;
}

inside the head.

I am unable to change the fonts. Please help.

If you could provide the sample source code for all the possibilities and for the samples that you have included inside your page http://www.allcolor.org/YaHPConverter/ with !New {date} and all, it will be great. It is difficult for me to understand the functionality of changing fonts, watermarking etc. Example codes will be appreciable.

allcolor commented 9 years ago

Hi, the property IHtmlToPdfTransformer.FOP_TTF_FONT_PATH must point to a directory containing the font(s), not to the font itself.

For the watermarking, you just have to use a PNG image with a transparency setting as a background image of the body.

Regards, Quentin Anciaux

2015-05-15 17:51 GMT+02:00 sibidharan notifications@github.com:

I am using this code

FileOutputStream out = null; try { CYaHPConverter converter = new CYaHPConverter(); if (!filename.endsWith(".pdf")) { filename = filename + ".pdf"; } File fout = new File(filename); out = new FileOutputStream(fout); Map properties = new HashMap(); List headerFooterList = new ArrayList(); headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter( "<table width=\"100%\"><td align=\"left\">"

  • "<td align=\"right\">Page /<"
  • "pagecount>", IHtmlToPdfTransformer.CHeaderFooter.HEADER)); headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter( "
    2015 © Jeevetha Decorators
    ", IHtmlToPdfTransformer.CHeaderFooter.FOOTER));
        properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
                IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
        properties.put(IHtmlToPdfTransformer.PDF_AUTHOR, "Jeevitha Decorators");
        properties.put(IHtmlToPdfTransformer.PDF_CREATOR, "Blovia Inc.");
        properties.put(IHtmlToPdfTransformer.PDF_TITLE, "Reference");
        properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, new File("calibri.ttf").getAbsolutePath());
        converter.convertToPdf(new URL(url),
                IHtmlToPdfTransformer.A4P, headerFooterList, out,
                properties);
        out.flush();
        out.close();
        return fout;
    } catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MalformedURLException ex) {
        JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IHtmlToPdfTransformer.CConvertException | IOException ex) {
        JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            out.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return null;

I placed the ttf file in the same folder. But the generated pdf is not embedding and displaying the font. I have also specified

*{ font-family:Calibri; font:Calibri; }

inside the head.

I am unable to change the fonts. Please help.

If you could provide the sample source code for all the possibilities and for the samples that you have included inside your page http://www.allcolor.org/YaHPConverter/ with !New {date} and all, it will be great. It is difficult for me to understand the functionality of changing fonts, watermarking etc. Example codes will be appreciable.

— Reply to this email directly or view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33.

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

sibidharan commented 9 years ago

Thank you :)

Regards, Sibidharan Nandhakumar, Chief Executive Officer, Blovia Inc. (Start-up) Official Mail: sibidharan@blovia.in http://facebook.com/blovia http://blovia.in (Under Construction) On 15-May-2015 9:50 pm, "Quentin Anciaux" notifications@github.com wrote:

Hi, the property IHtmlToPdfTransformer.FOP_TTF_FONT_PATH must point to a directory containing the font(s), not to the font itself.

For the watermarking, you just have to use a PNG image with a transparency setting as a background image of the body.

Regards, Quentin Anciaux

2015-05-15 17:51 GMT+02:00 sibidharan notifications@github.com:

I am using this code

FileOutputStream out = null; try { CYaHPConverter converter = new CYaHPConverter(); if (!filename.endsWith(".pdf")) { filename = filename + ".pdf"; } File fout = new File(filename); out = new FileOutputStream(fout); Map properties = new HashMap(); List headerFooterList = new ArrayList(); headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter( "<table width=\"100%\"><td align=\"left\">"

  • "<td align=\"right\">Page /<"
  • "pagecount>", IHtmlToPdfTransformer.CHeaderFooter.HEADER)); headerFooterList.add(new IHtmlToPdfTransformer.CHeaderFooter( "
    2015 © Jeevetha Decorators
    ", IHtmlToPdfTransformer.CHeaderFooter.FOOTER));

properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS, IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER); properties.put(IHtmlToPdfTransformer.PDF_AUTHOR, "Jeevitha Decorators"); properties.put(IHtmlToPdfTransformer.PDF_CREATOR, "Blovia Inc."); properties.put(IHtmlToPdfTransformer.PDF_TITLE, "Reference"); properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, new File("calibri.ttf").getAbsolutePath()); converter.convertToPdf(new URL(url), IHtmlToPdfTransformer.A4P, headerFooterList, out, properties); out.flush(); out.close(); return fout; } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE); Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex); } catch (MalformedURLException ex) { JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE); Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex); } catch (IHtmlToPdfTransformer.CConvertException | IOException ex) { JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE); Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex); } finally { try { out.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(this, ex.getLocalizedMessage(), "Error", JOptionPane.ERROR_MESSAGE); Logger.getLogger(Customer.class.getName()).log(Level.SEVERE, null, ex); } } return null;

I placed the ttf file in the same folder. But the generated pdf is not embedding and displaying the font. I have also specified

*{ font-family:Calibri; font:Calibri; }

inside the head.

I am unable to change the fonts. Please help.

If you could provide the sample source code for all the possibilities and for the samples that you have included inside your page http://www.allcolor.org/YaHPConverter/ with !New {date} and all, it will be great. It is difficult for me to understand the functionality of changing fonts, watermarking etc. Example codes will be appreciable.

— Reply to this email directly or view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33.

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

— Reply to this email directly or view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33#issuecomment-102451931 .

milkdeliver commented 9 years ago

Hi, Does it works If I would use webcontent file ? If so how could I access it?thank you so much. image

Best Regards

allcolor commented 9 years ago

Hi,

as long as you set the property to the absolute directory containing the font file, it will work. If in a servlet environment, you can have the value through servletcontext.getRealPath()... If you run in an environment where that method returns null, then your only option is to put the ttf dir externally from you web app and have a runtime property that contains the absolute path to the directory.

Regards, Quentin

2015-08-05 11:11 GMT+02:00 Sam notifications@github.com:

Hi, Does it works If I would use webcontent file ? [image: image] https://cloud.githubusercontent.com/assets/3108407/9082168/b3d2d92a-3b94-11e5-8a9a-e0cbff3950e8.png

Best Regards

— Reply to this email directly or view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33#issuecomment-127926430 .

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

cleger35 commented 8 years ago

I am having trouble getting my font to change as well in my PDF. I am using the following code:

public static ByteArrayOutputStream createPdfFromHtml(String htmlString) throws Exception {
        CYaHPConverter converter = new CYaHPConverter();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        Map properties = new HashMap();
        List headerFooterList = new ArrayList();

        IHtmlToPdfTransformer.PageSize pageSize = new IHtmlToPdfTransformer.PageSize(21, 29.7, 1, 1, .5, .5);  // note: this is A4 with 1 cm side and .5 cm top/bottom margins
        properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
                IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
        properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "http://localhost:8100/references/Arial_Temp/");
        converter.convertToPdf(htmlString,
                pageSize,
                //IHtmlToPdfTransformer.A4P,
                headerFooterList,
                // Use the line below for development
                "file:\\http://localhost:8100/references/pdf-styling.css", // root for relative external CSS and IMAGE
                // Use the line below for production
//                "file:\\http://192.168.32.84:7001/references/pdf-styling.css",
                outputStream,
                properties);
        return outputStream;
    }

Here is my CSS:

@font-face {
    font-family: Arial;
    src: url(Arial_Temp/arial.ttf) format("truetype");
}

All other aspects of my css are working, and I know the font is being deployed as well. Do you have any advice?

Thanks in advance, Caleb

allcolor commented 8 years ago

Hi,

IHtmlToPdfTransformer.FOP_TTF_FONT_PATH must point to a filesystem directory accessible from the program not an url.

Regards,

Quentin

2016-05-26 17:31 GMT+02:00 Caleb Leger notifications@github.com:

I am having the same issue of my font not changing on the pdf. Here is the code that I'm using.

`public static ByteArrayOutputStream createPdfFromHtml(String htmlString) throws Exception { CYaHPConverter converter = new CYaHPConverter(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Map properties = new HashMap(); List headerFooterList = new ArrayList();

IHtmlToPdfTransformer.PageSize pageSize = new IHtmlToPdfTransformer.PageSize(21, 29.7, 1, 1, .5, .5);  // note: this is A4 with 1 cm side and .5 cm top/bottom margins
properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
        IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);
properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "http://localhost:8100/references/Arial_Temp/");
converter.convertToPdf(htmlString,
        pageSize,
        //IHtmlToPdfTransformer.A4P,
        headerFooterList,
        // Use the line below for development
        "file:\\http://localhost:8100/references/pdf-styling.css", // root for relative external CSS and IMAGE
        // Use the line below for production

// "file:\http://192.168.32.84:7001/references/pdf-styling.css", outputStream, properties); return outputStream; }`

Here is the CSS that I am using:

@font-face { font-family: Arial; src: url(Arial_Temp/arial.ttf) format("truetype"); }

All other aspects of my css are working, and I know the font is being deployed as well. Do you have any advice?

Thanks in advance, Caleb

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33#issuecomment-221906379

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

cleger35 commented 8 years ago

Is it necessary to add CSS to it in order to get it to work or can it be done straight from that one line in the method? I tried this, but to no avail... :

 properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "C:/Users/cleger/Desktop/Arial_Temp/");
allcolor commented 8 years ago

You have to set the path where the ttf font files are located, and then you have to set the font family through an inline style or a css rule.

Regards Le 26 mai 2016 20:21, "Caleb Leger" notifications@github.com a écrit :

Is it necessary to add CSS to it in order to get it to work or can it be done straight from that one line in the method? I tried this, but to no avail... :

properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "C:/Users/cleger/Desktop/Arial_Temp/");

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33#issuecomment-221952957

cleger35 commented 8 years ago

Okay so I moved the font file to a location in the project and am referencing it like this:

properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "C:/Users/cleger/IdeaProjects/Laitram Web Services/CrossPlatformClient/LaitramApp/www/references/Arial_Temp/");

I then added the following CSS rules:

@font-face {
    font-family: Arial;
    src: url(Arial_Temp/arial.ttf) format("truetype");
}

* {
    font-family: Arial !important;
}

Still no luck, and I'm not sure exactly what else to do. Is my code not correct?

allcolor commented 8 years ago

Try to put style="font-family: Arial" on an enclosing tag and see if it works.

2016-05-27 17:14 GMT+02:00 Caleb Leger notifications@github.com:

Okay so I moved the font file to a location in the project and am referencing it like this:

properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "C:/Users/cleger/IdeaProjects/Laitram Web Services/CrossPlatformClient/LaitramApp/www/references/Arial_Temp/");

I then added the following CSS rules:

@font-face { font-family: Arial; src: url(Arial_Temp/arial.ttf) format("truetype"); }

  • { font-family: Arial !important; }

Still no luck, and I'm not sure exactly what else to do. Is my code not correct?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33#issuecomment-222173288, or mute the thread https://github.com/notifications/unsubscribe/ABJ5B-IgAMJGnxYH9lDc22hq17DEvNl-ks5qFwpGgaJpZM4Eb11O .

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

allcolor commented 8 years ago

Like here http://www.allcolor.org/YaHPConverter/samples/test2.html

2016-05-27 17:21 GMT+02:00 Quentin Anciaux allcolor@gmail.com:

Try to put style="font-family: Arial" on an enclosing tag and see if it works.

2016-05-27 17:14 GMT+02:00 Caleb Leger notifications@github.com:

Okay so I moved the font file to a location in the project and am referencing it like this:

properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "C:/Users/cleger/IdeaProjects/Laitram Web Services/CrossPlatformClient/LaitramApp/www/references/Arial_Temp/");

I then added the following CSS rules:

@font-face { font-family: Arial; src: url(Arial_Temp/arial.ttf) format("truetype"); }

  • { font-family: Arial !important; }

Still no luck, and I'm not sure exactly what else to do. Is my code not correct?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33#issuecomment-222173288, or mute the thread https://github.com/notifications/unsubscribe/ABJ5B-IgAMJGnxYH9lDc22hq17DEvNl-ks5qFwpGgaJpZM4Eb11O .

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

allcolor commented 8 years ago

http://www.allcolor.org/YaHPConverter/samples/test2.pdf

Please note that the font-family must be exactly what you would get from loading the font in java and printing out its name. If it's not exactly the same it won't work.

Regards

2016-05-27 17:22 GMT+02:00 Quentin Anciaux allcolor@gmail.com:

Like here http://www.allcolor.org/YaHPConverter/samples/test2.html

2016-05-27 17:21 GMT+02:00 Quentin Anciaux allcolor@gmail.com:

Try to put style="font-family: Arial" on an enclosing tag and see if it works.

2016-05-27 17:14 GMT+02:00 Caleb Leger notifications@github.com:

Okay so I moved the font file to a location in the project and am referencing it like this:

properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, "C:/Users/cleger/IdeaProjects/Laitram Web Services/CrossPlatformClient/LaitramApp/www/references/Arial_Temp/");

I then added the following CSS rules:

@font-face { font-family: Arial; src: url(Arial_Temp/arial.ttf) format("truetype"); }

  • { font-family: Arial !important; }

Still no luck, and I'm not sure exactly what else to do. Is my code not correct?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/allcolor/YaHP-Converter/issues/33#issuecomment-222173288, or mute the thread https://github.com/notifications/unsubscribe/ABJ5B-IgAMJGnxYH9lDc22hq17DEvNl-ks5qFwpGgaJpZM4Eb11O .

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

All those moments will be lost in time, like tears in rain. (Roy Batty/Rutger Hauer)

cleger35 commented 8 years ago

Ah, I finally got it to work! Thanks a ton!

Thanks, Caleb