vb64 / markdown-pdf

Markdown to pdf renderer
MIT License
22 stars 2 forks source link

Custom CSS #5

Closed Tomisall closed 4 months ago

Tomisall commented 5 months ago

Hi, sorry if I'm missing the obvious here. How would I feed in custom CSS (I really just want to centre img's)? Cheers for any help.

Tomisall commented 5 months ago

Ah for my needs wrapping the img's in <div style="text-align: center;">...</div> worked, I should have figured. Still could be cool to modify add_section to optionally accept CSS as an argument to pass to fitz.Story.

vb64 commented 5 months ago

Yes, some html injections work for markdown file.

Centered text, for example:

<p style="text-align: center;">Centered text</p>

vb64 commented 5 months ago

Still could be cool to modify add_section to optionally accept CSS as an argument to pass to fitz.Story.

Can you prepare a pull request for such a change?

Or explain in more detail, with an example, the functionality you need?

Tomisall commented 5 months ago

Oh I just mean something like:

    def add_section(self, section, css):
        """Add markdown section to pdf."""
        rect = fitz.paper_rect(section.paper_size)
        where = rect + section.borders
        story = fitz.Story(html=self.m_d.render(section.text), user_css=css, archive=section.root)
        more = 1
        while more:  # loop outputting the story
            self.page += 1
            device = self.writer.begin_page(rect)
            more, _ = story.place(where)  # layout into allowed rectangle
            story.element_positions(self.recorder, {"toc": section.toc, "pdfile": self})
            story.draw(device)
            self.writer.end_page()

Of course that would now mean you would always have to pass css but that could be blank. Or could be more flexible if it wasn't an argument for add_section but was an attribute of MarkdownPDF (empty string by default) and then users could optionally define it to be whatever their css is:

    def add_section(self, section):
        """Add markdown section to pdf."""
        rect = fitz.paper_rect(section.paper_size)
        where = rect + section.borders
        story = fitz.Story(html=self.m_d.render(section.text), user_css=self.css, archive=section.root)
        more = 1
        while more:  # loop outputting the story
            self.page += 1
            device = self.writer.begin_page(rect)
            more, _ = story.place(where)  # layout into allowed rectangle
            story.element_positions(self.recorder, {"toc": section.toc, "pdfile": self})
            story.draw(device)
            self.writer.end_page()

Nothing too fancy and totally your call if you want to include something like this but it could offer more flexibility if users wanted to add custom css to format table styles or whatever without having to include it in the markdown 😄

vb64 commented 4 months ago

Nothing too fancy and totally your call if you want to include something like this but it could offer more flexibility if users wanted to add custom css to format table styles or whatever without having to include it in the markdown 😄

You can update markdown-pdf up to version 1.1 and use as follows:

pdf.add_section(Section("# Head1\n\nbody\n"), user_css="h1 {text-align:center;}")

Tomisall commented 4 months ago

Ah very cool. Thank you :)