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

How to start a new page inside a tag? #209

Open
Ayush1325 opened this issue Oct 27, 2020 · 10 comments
Open

How to start a new page inside a tag? #209

Ayush1325 opened this issue Oct 27, 2020 · 10 comments

Comments

@Ayush1325
Copy link

Okay, So I am creating a big PDF programmatically but it seems pagebreak can only be used at the top level. So how can I start a new page if I am inside a list or maybe a chapter? Are there tags or something for section? Or maybe I can just use chapter for whatever requires a new page inside a chapter? I want to get the auto numbering the way we get in chapters and list. I know I can just do it programmatically and just use all the top-level elements, but I would like to know if there is a better solution.

@yogthos
Copy link
Collaborator

yogthos commented Oct 28, 2020

Doing it programmatically would be the way to go at the moment. I'm open to adding the feature if it is supported by the openpdf API though. I won't have time to look in the near future, but I can help with a PR for this.

@Ayush1325
Copy link
Author

Ok, thanks. I will work on it if possible.

@Ayush1325
Copy link
Author

Ok, so I tried the rudimentary approach of using a global doc variable and create a new page by (.newPage doc) but that does not seem to work inside the :section render method (does work with :pagebreak). Also there seem to be doc-events in the core.clj:

(defn doc-events [^PdfWriter pdf-writer
                  {:keys [on-document-open
                          on-document-close
                          on-page-start
                          on-page-end
                          on-chapter-start
                          on-chapter-end
                          on-paragraph-start
                          on-paragraph-end
                          on-section-start
                          on-section-end
                          event-handler]}]
  (if event-handler
    (.setPageEvent pdf-writer ^PdfPageEventHelper event-handler)

    (when (not-empty (remove nil? [on-document-open
                                   on-document-close
                                   on-page-start
                                   on-page-end
                                   on-chapter-start
                                   on-chapter-end
                                   on-paragraph-start
                                   on-paragraph-end
                                   on-section-start
                                   on-section-end
                                   event-handler]))
      (.setPageEvent
        pdf-writer
        ^PdfPageEventHelper
        (proxy [PdfPageEventHelper] []
          (onOpenDocument [^PdfWriter writer ^Document doc]
            (when on-document-open
              (on-document-open writer ^Document doc)))

          (onCloseDocument [^PdfWriter writer ^Document doc]
            (when on-document-close
              (on-document-close writer doc)))

          (onStartPage [^PdfWriter writer ^Document doc]
            (when on-page-start
              (on-page-start writer doc)))

          (onEndPage [^PdfWriter writer ^Document doc]
            (when on-page-end
              (on-page-end writer doc)))

          (onChapter [^PdfWriter writer ^Document doc position ^Paragraph title]
            (when on-chapter-start
              (print "New Chapter")
              (on-chapter-start writer doc position title)))

          (onChapterEnd [^PdfWriter writer ^Document doc position]
            (when on-chapter-end
              (on-chapter-end writer doc position)))

          (onParagraph [^PdfWriter writer ^Document doc position]
            (when on-paragraph-start
              (on-paragraph-start writer ^Document doc position)))

          (onParagraphEnd [^PdfWriter writer ^Document doc position]
            (when on-paragraph-end
              (on-paragraph-end writer doc position)))

          (onSection [^PdfWriter writer ^Document doc position depth ^Paragraph title]
            (when on-section-start
              (on-section-start writer doc position depth title)))

          (onSectionEnd [^PdfWriter writer ^Document doc position]
            (when on-section-end
              (on-section-end writer doc position))))))))

and it has a section on-section-started, but it seems to be called with all nil arguments, at least in the debugger, so what's up with that?

@yogthos
Copy link
Collaborator

yogthos commented Oct 29, 2020

Hmm, not sure why you're not seeing the arguments. If I run the following, I'm seeing what's expected:

(pdf
   [{:on-section-start (fn [writer doc position depth title]
                         (println "args:" writer doc position depth title))}
    [:chapter "chapter"
     [:section "Section Title" "Some content"]]]
   "doc.pdf")

=> args: #object[com.lowagie.text.pdf.PdfWriter 0x5dec2391 com.lowagie.text.pdf.PdfWriter@5dec2391] #object[com.lowagie.text.pdf.PdfDocument 0x58b3e0e com.lowagie.text.pdf.PdfDocument@58b3e0e] 791.0 2 #object[com.lowagie.text.Paragraph 0x51e758c4 [1.1. , Section Title]]

@Ayush1325
Copy link
Author

Hmm, not sure why you're not seeing the arguments. If I run the following, I'm seeing what's expected:

(pdf
   [{:on-section-start (fn [writer doc position depth title]
                         (println "args:" writer doc position depth title))}
    [:chapter "chapter"
     [:section "Section Title" "Some content"]]]
   "doc.pdf")

=> args: #object[com.lowagie.text.pdf.PdfWriter 0x5dec2391 com.lowagie.text.pdf.PdfWriter@5dec2391] #object[com.lowagie.text.pdf.PdfDocument 0x58b3e0e com.lowagie.text.pdf.PdfDocument@58b3e0e] 791.0 2 #object[com.lowagie.text.Paragraph 0x51e758c4 [1.1. , Section Title]]

Ok, thanks. I was trying to pass it the wrong way. I will try to see if I can use that in some way to start a new page, although I think it might not be possible to do with quick tricks since even directly adding (.newPage doc) in the subtags doesn't seem to work. Maybe will have to see if openpdf supports it.

@yogthos
Copy link
Collaborator

yogthos commented Oct 29, 2020

Yeah, I think you'll have to check what openpdf can do here, and then we can expose that API.

@Ayush1325
Copy link
Author

Can confirm, using (.newPage doc) in the on-section-start seems to work, even with subsections. That makes me wonder why it doesn't work using a global variable or something.

@yogthos
Copy link
Collaborator

yogthos commented Oct 30, 2020

It's likely dependent on the state of the doc. So, it sounds like it's possible to use on-section-start to trigger page breaks then, but you have to manually figure out what section you're in and whether you want to do a page break or not.

@Ayush1325
Copy link
Author

Yeah, I can do it easily for my particular case, but don't really know any general implementation since the on-section-start function doesn't really get any meta of section either, just the title and numbering.

@yogthos
Copy link
Collaborator

yogthos commented Oct 30, 2020

Looks like that's the best approach at the moment, glad to hear it solves your use case.

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