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

Added the ability to overlay one PDF over another #63

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

brianbutton
Copy link

Description of your pull request

Added the ability to overlay one PDF over another

Pull request checklist

Before submitting the PR make sure the following things have been done
(and denote this by checking the relevant checkboxes):

  • The code is consistent with Clojure style guide.
  • All code passes the linter (clj-kondo --lint src).
  • You've added tests (if possible) to cover your change(s).
  • All tests are passing.
  • The commits are consistent with the Git commit style guide.
  • You've updated the changelog (if adding/changing user-visible functionality).

Thanks!

@dotemacs
Copy link
Owner

Thanks for doing this, I'll have a look over the weekend!

(.setEvenPageOverlayFile overlay even-page-overlay-file-path)
(.setEvenPageOverlayPDF overlay even-page-overlay-pdf)

(-> overlay (.overlay {}) (.save output-file-path))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for hijacking the thread, I thought I'd offer some tips... Hope it's ok 🙂

So yeah, this is dotemacs' call but you might want to avoid saving to disk since that's a side-effect (and side-effects make writing proper tests harder, among other things)

I'd also argue that returning a byte array would be more useful to the end-user. Every user of your function is interested in the output, one way or the other, but how many will be interested in storing it locally right away?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Thank you for the feedback. I agree a byte array would be more useful in general but would mean its different from the other functions for example draw, form and merge all save to disk.
I'm happy to have it either way. @dotemacs is there reason to save to disk here or can I change this to return a byte array?

Comment on lines +10 to +27
(deftest pdf-file-merge
(let [_
(overlay-pdf
{:input-file-path "test/pdfs/clojure-1.pdf"
:overlay-file-path "test/pdfs/clojure-2.pdf"
:output-file-path output-file-path})

overlayed-pdf-file (.exists (io/as-file output-file-path))]
(is (true? overlayed-pdf-file))
(is (true? (common/is-pdf? output-file-path)))))

;; clean up
(defn clean-up [file]
(if (.exists (io/as-file file))
(io/delete-file file)))

(deftest cleaner
(clean-up "test/pdfs/foo.pdf"))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more feedback here as well!

  1. Keep in mind that not all test runners are deterministic! I don't know about clj test-runner but others like kaocha allow you to randomize the test run sequence to catch side-effects. Therefore you should not make assumptions about the order tests run. Consider cleaning up the temporary file in the deftest it was created ;).
  2. What if the output file already exists for some reason unrelated to these tests? Your tests will overwrite that and then delete it. That shouldn't happen. As an alternative, take a look at this: https://stackoverflow.com/a/57845906

You could also consider the following function:

(defn with-temp-pdf [prefix fn]
  (let [file (java.io.File/createTempFile prefix ".pdf")]
    (fn (str file))
    (if (.exists file)
      (io/delete-file file))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good points! I followed this pattern from the tests in merge.
I'll update once the byte array vs output file is finalised.

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

Successfully merging this pull request may close these issues.

None yet

4 participants