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

Not showing style colour in PDF #8

Closed derrick2012 closed 10 years ago

derrick2012 commented 11 years ago

Hi there,

I used YaHP-Converter to convert the following HTML to PDF but I found the style colour green was not showing.

styletest

Here is my jsp code:

        File htmlIn = new File(filename);
        Scanner scanner = new Scanner(htmlIn).useDelimiter("\\Z");
        String htmlContents = scanner.next();
        CYaHPConverter converter = new CYaHPConverter();
        Map properties = new HashMap();
        List headerFooterList = new ArrayList();
        properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS, IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);

        OutputStream os = response.getOutputStream();
        response.reset();
        response.setContentType("application/pdf");
        String[] fname = filename.split("\\\\");
        if (fname[0].equals(filename)) {
           fname = filename.split("/");
        }
        response.setHeader("Content-Disposition", "attachment; filename= \""+fname[fname.length-1]+".pdf\"");
        converter.convertToPdf(htmlContents,
            IHtmlToPdfTransformer.A4P,
            headerFooterList,
            "file:///temp/html/",
            os,
            properties);

        response.flushBuffer();

Please let me know if I did anything wrong. Thanks a lot!

allcolor commented 11 years ago

Could you send me your html ? (as html, not an image)

Thank you

2013/6/6 derrick2012 notifications@github.com

Hi there,

I used YaHP-Converter to convert the following HTML to PDF but I found the style colour green was not showing.

[image: styletest]https://f.cloud.github.com/assets/2483552/617515/302e7f8c-ce97-11e2-8993-9f44fcc014b1.png

Here is my jsp code:

    File htmlIn = new File(filename);
    Scanner scanner = new Scanner(htmlIn).useDelimiter("\\Z");
    String htmlContents = scanner.next();
    CYaHPConverter converter = new CYaHPConverter();
    Map properties = new HashMap();
    List headerFooterList = new ArrayList();
    properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS, IHtmlToPdfTransformer.FLYINGSAUCER_PDF_RENDERER);

    OutputStream os = response.getOutputStream();
    response.reset();
    response.setContentType("application/pdf");
    String[] fname = filename.split("\\\\");
    if (fname[0].equals(filename)) {
       fname = filename.split("/");
    }
    response.setHeader("Content-Disposition", "attachment; filename= \""+fname[fname.length-1]+".pdf\"");
    converter.convertToPdf(htmlContents,
        IHtmlToPdfTransformer.A4P,
        headerFooterList,
        "file:///temp/html/",
        os,
        properties);

    response.flushBuffer();

Please let me know if I did anything wrong. Thanks a lot!

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

All those moments will be lost in time, like tears in rain.

derrick2012 commented 11 years ago

I do not know how to attach the file as html. Here is the html content:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Example To Convert HTML File with Images to PDF Using Java iText Transform HTML document with Pictures We have converted a HTML File with Images to PDF document Using Java!!
Let us add an image to the HTML Document
allcolor commented 11 years ago

Hi,

I've done a test here and all is working as expected.

I've attached the resulting PDF and html.

2013/6/7 derrick2012 notifications@github.com

I do not know how to attach the file as html. Here is the html content:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Example To Convert HTML File with Images to PDF Using Java iText b { color: green; }

Transform HTML document with Pictures We have converted a HTML File with Images to PDF document Using Java!!

Let us add an image to the HTML Document

— Reply to this email directly or view it on GitHubhttps://github.com/allcolor/YaHP-Converter/issues/8#issuecomment-19084185 .

All those moments will be lost in time, like tears in rain.

allcolor commented 11 years ago

Well the attachment did not pass. So here is the html: http://www.allcolor.org/test.html and the resulting pdf: http://www.allcolor.org/test.pdf

allcolor commented 11 years ago

Hmm... no I've found a problem... in fact in the html I've forgotten to close the style tag and then it did work. If I correctly close it, it doesn't. I'm looking into the problem now.

derrick2012 commented 11 years ago

Thank you so much for your help!

allcolor commented 11 years ago

Ok, the thing is all b tag are replaced by strong tag by the validator.

So ==> <style type="text/css"> b,strong { color : green; } </style>

allcolor commented 11 years ago

To avoid any problem, it is best to use class (or id) instead of directly stiling a tag type.

derrick2012 commented 11 years ago

Do you know why tag
would add extra page break in the PDF? When I used flying saucer to convert the same html, it did not add extra page break in the PDF.

derrick2012 commented 11 years ago

Do you know why tag "< br />" would add extra page break in the PDF? When I used flying saucer to convert the same html, it did not add extra page break in the PDF.

allcolor commented 11 years ago

a br where ? Have you an example of that ? and a page break ?? br doesn't do a page break.

derrick2012 commented 11 years ago

Please take a look at my HTML image file above, there is a br tag in the 12th line, just after the line "We have converted a HTML File with Images to PDF document Using Java!!".

allcolor commented 11 years ago

Ok.. and what happen at this br tag ?

derrick2012 commented 11 years ago

This tag caused an extra page break in the PDF using YaHP to convert. However, when I used flying saucer to convert, it did not have this extra page break. I am just curious what caused this difference.

allcolor commented 11 years ago

I've done what you asked here: So here is the html: http://www.allcolor.org/test.html and the resulting pdf: http://www.allcolor.org/test.pdf

I don't see the problem.

derrick2012 commented 11 years ago

Well, could you try again using the following image please? marbles

allcolor commented 11 years ago

It's because of the size of the image, not the <br/> tag.

Size it to fit the page and all works well as you can see in the sample here:

http://www.allcolor.org/test.pdf http://www.allcolor.org/test.html

2013/6/7 derrick2012 notifications@github.com

Well, could you try again using the following image please? [image: marbles]https://f.cloud.github.com/assets/2483552/624119/c800977e-cf7c-11e2-9895-861a81955219.png

— Reply to this email directly or view it on GitHubhttps://github.com/allcolor/YaHP-Converter/issues/8#issuecomment-19109479 .

All those moments will be lost in time, like tears in rain.

derrick2012 commented 11 years ago

just wonder why it does not behave the same like flying saucer. is it because flying saucer has a different page size setting?

allcolor commented 11 years ago

Maybe, or that you don't use the same version... Don't know. but your image doesn't fit in a page (it's 35 cm high). Also yahp by default (if not changed) add a 1cm margin, so you really only have 27 cm high. It tries not to cut in the middle.. so it puts the image on the second page.

You shoud use styling to set a correct size to fit in the page.

Regards, Quentin

2013/6/7 derrick2012 notifications@github.com

just wonder why it does not behave the same like flying saucer. is it because flying saucer has a different page size setting?

— Reply to this email directly or view it on GitHubhttps://github.com/allcolor/YaHP-Converter/issues/8#issuecomment-19110690 .

All those moments will be lost in time, like tears in rain.

derrick2012 commented 11 years ago

I guess it's probably due to the margin setting. Anyway, thanks a lot for your help!