Skip to content
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

Custom PDF configuration #6

Open
LaurentFLEIFEL opened this issue Jul 6, 2017 · 6 comments
Open

Custom PDF configuration #6

LaurentFLEIFEL opened this issue Jul 6, 2017 · 6 comments

Comments

@LaurentFLEIFEL
Copy link

Hello,

Is it possible to add some custom pdf configuration ?

Like changing the first page or justify the text, center formula, etc.

Best regards

@Arnauld
Copy link
Owner

Arnauld commented Jul 6, 2017

Hi!

Yes it is, depends on what you want to accomplish you will need to write some code.

To change the default cover page, you just have to provide your own:

new DefaultPdfReportBuilder()
...
                .coverPage(new MyCoverPage())
                .subTitle("Technical & Functional specifications")

MyCoverPage needs to implements gutenberg.itext.SimpleEmitter, you may inspire yourself from the default CoverPage: FirstPageRenderer

Justifying text is a bit trickier since it depends on which text you want to justify:

  • text from paragraph in markdown content
  • text of steps

Font color and size can also be customized per component e.g.

new DefaultPdfReportBuilder()
                .using(new Configuration()
                                .adjustFont(Styles.TABLE_HEADER_FONT, new FontModifier().size(10.0f))
                                .adjustFont(Styles. CODE_FONT, new FontModifier().size(10.0f))
                )

Keys to use may be found in Styles;
it contains all key dedicated to markdown and html based semantics e.g. level 1, 2, 3 titles, table font, ...

You may need to look at dedicated emitter for all scenario based rendering, e.g. for steps rendering StepsEmitter. STEP_KEYWORD_FONT

new DefaultPdfReportBuilder()
                .using(new Configuration()
                                .adjustFont(Styles.TABLE_HEADER_FONT, new FontModifier().size(10.0f))
                                .adjustFont(Styles. CODE_FONT, new FontModifier().size(10.0f))
                                .adjustFont(StepsEmitter. STEP_KEYWORD_FONT, new FontModifier().bold().size(10.0f))
                )

Hope this helps,
//Arnauld

@LaurentFLEIFEL
Copy link
Author

Hi,
Thanks for the fast answer.

It helps, especially for the cover page, and I managed to add bookmarks from the table of contents with some custom Emitter, but I have to admit the text justification is beyond my reach for now.
Should I create also a custom Emitter for it ? I want to justify markdown text.
Is it the same to center formulas (from ```formula parts) ?

I don't see how by an emitter I can detect that it will handle some markdown text (or formula text). (I'm not sure exactly how emitter works in the pdf generation)

Thanks again,

Laurent

@Arnauld
Copy link
Owner

Arnauld commented Jul 6, 2017

markdown is 'Emitted' by MarkdownEmitter

The complex part is that markdown is a complex structure that will 'sub-emit' fragments recursively (table, paragraph, list, bold, link...).
Markdown is parsed which is performed by pegdown library; the 'sub-emit' is done by something called Processor, you may find them in pegdown processors package.
Processor approach is a bit more complex since it requires that processors may also invoke processors, one actually traverse the markdown AST.

For example the paragraph processor (ParaNodeProcessor delegates the processing of its children for bold, italic or nested list...

By looking at the code, several years afterward, it seems that:

  1. With actual code, It's not possible to override ParagraphProcessor; this requires some change in the code, here: InvocationContext and here MarkdownEmitter to allow external customization
  2. I now remember that you can try to add attributes to paragraph, by using the following syntax in your markdown {align:right}; unfortunately Justified is not supported 😞 (switch case here and here)

An example here:
https://raw.githubusercontent.com/Arnauld/gutenberg/master/src/test/resources/gutenberg/pegdown/paragraph-02.md
corresponding test here
https://github.com/Arnauld/gutenberg/blob/master/src/test/java/gutenberg/itext/PegdownPdfTest.java#L304

@Arnauld
Copy link
Owner

Arnauld commented Jul 6, 2017

Formula is different, similar to ditaa diagram, rendering is drawn in the pdf directly. So that it is a vector which scale and render nicely when zooming in.

https://github.com/Arnauld/gutenberg/blob/master/src/main/java/gutenberg/itext/emitter/SourceCodeLaTeXExtension.java

@LaurentFLEIFEL
Copy link
Author

Hello,
Thanks for all the information.
I managed to justify the markdown text by using aop around the method applyAlign you mentionned.
This is an ugly workaround and every paragraph that need to be justify have to be precede by {align:left} to be taken into account.

Thanks again !

Laurent

@Arnauld
Copy link
Owner

Arnauld commented Jul 7, 2017

You are welcome!
I am strongly interested to see your result, could you share one of such pdf (with real world examples 😛 )!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants