-
Notifications
You must be signed in to change notification settings - Fork 36
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
Add merge feature #194
Add merge feature #194
Conversation
Hey!
I also thought on the possibility to add something else so we could print and merge multiple PDFs from different HTML templates. Any idea on this? Thanks a lot! |
Hey @xaviRodri Thanks for your contribution! I think a merge function is indeed a nice little addition - even though people keep telling me that the Ghostscript part of ChromicPDF should actually be its own library 🙃 I'll take some time looking into your changes later. Already have 2 questions for you though:
No strong opinion, but probably would go with the pre-generated files in
We have the
Naming tbd :) |
Thanks for the fast answer @maltoe !! 💪 First, to answer to your questions:
It depends on 2 factors:
I'm not super familiar with all the PDF/A features.. but I tried to see an example file with Also, I like a lot the idea to get a list of source and options and create a Thanks again! 💯 |
@xaviRodri Thought about it some more and figured we could as well just extend the existing New signature would be:
Behaviour:
Things to look out for:
-> With this new function I think we should not add a top-level Next, since we're using ghostscript for the merge anyway, we would be able to use the existing ghostscript functionality, i.e. adding metadata (see |
Thanks for taking a look!! I'm right now trying to get it work 👍 what do you think about this spec? @spec print_to_pdf(source() | source_and_options() | [source() | source_and_options()], [export_option()]) :: export_return() So the current people using it don't need to change it into a list? |
Yes, sorry, this is what I had in mind, too. And yes, let's absolutely make sure to not break existing API ;) |
Some updates @maltoe ! I got it working! 🎊 I'm running into a "problem" that I've solved but maybe some help could improve the solution.. So, right now, if we use But I was thinking on the possibility of merging both instead of overriding them, so we would be able to have some general attributes (e.g. margins for the whole merged pdf) but at the same time specify some for a particular "section" of the final file (e.g. I want the top margin bigger in this section, I want a different header for that section, etc.). Let me push these draft changes so you can check the idea better (don't expect a super clean code, specs and so as it needs still some work!) |
lib/chromic_pdf/api.ex
Outdated
|
||
tmp_path | ||
end) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can DRY up this function a bit by extracting the identical "branches" of the anonymous function into another function
@xaviRodri Nice work already! |
I will play with it today too so I can address all the above 🔝 But I think we are now on the good direction 💯 Thanks a lot for the feedback till now 🙏 |
More updates! 👀 I have a couple of questions before writing documentation & tests:
For the rest, if you have a sec to check it, I'm happy to change anything you find weird 👍 |
In order to be able to merge multiple PDF files, we have added this Ghostscript command that can be called using `ChromicPDF.merge/2`. This new function accepts a list of paths and an output option that can be added or not depending on the desired output (same as `print_to_pdf/2` output option).
In order to be able to print and merge multiple sources, we have done the following: * Added a temporary `print_to_pdf_and_merge/2` function that will, in a tmp dir, create all the individual PDF files and then merge them * Updated the way the `print_to_pdf` options are merged (so we don't just override them) * Update the `source_and_options/1` function so it accepts more style options (margins) * Update the `source_and_options/1` so you can specify that your styles will be written in your templates (`style: :custom` option) * Added a `PDFOptions.feed_merge_data_into_output/2` similar to `PDFAOptions.feed_ghostscript_file_into_output/2`
In order to have a single module where we manage how to output PDF files, we have created `ChromicPDF.OutputOptions` (using plural as we also do it for `PDFOptions`). In here we can find the previous `feed_chrome_data_into_output/2` and a new `feed_file_into_output/2` that replaces `PDFAOptions.feed_ghostscript_file_into_output/2` . That's because we will also use the same function to output merged PDF files.
As the feature is the same, just that getting an input with multiple sources, we have joined both functions into one. Also updated the specs to match this change.
b9c6ec3
to
c0325e7
Compare
Hmm, wasn't aware of this anymore. OK then, maybe we align with the existing behaviour and continue to send one
Let's get rid of it for now. If someone wants to only merge PDFs, they can use |
We will use the merge functionality internally only.
As we already get the full path, we don't need to expand it in this case.
Added an event around the merge operation in `print_to_pdf/3` to align with the current behaviour in other processes.
@xaviRodri Looking good, thanks! When you document this, please make sure to mention that Ghostscript is required for the merge step and that printing of individual files before merge is done sequentially. For testing, I'd suggest to add a test case to |
I wonder if it would be worth adding a ticket after this to allowing parallel printing here (at least up to the pool size?). Wdyt @maltoe |
I think that is more complex than we foresee now, but yeah, we could explore it for sure. |
Minimal tests so we only cover the specific feature. The rest is already covered by other tests.
Hey again! Added some tests and documentation as we agreed! Also about the documentation: I added it inside "Input options" as this is related to the inputs. But if you think it should belong to another part let me know! 👍 Marking it as "Ready for review"! 🚀 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is great!
Since we already had a few iterations - and since I realized that I want to refactor a few things (types, docs, Ghostscript module) -, I'd merge this as-is and "own" your code in a follow-up commit. Hope you don't mind.
Thanks a lot for the contribution! 💜 💙 💛 Super happy about how this turned out. Very nice addition, especially given we've recently been asked about this very feature in #177
**This feature requires Ghostscript to merge different files.** | ||
|
||
If you have multiple inputs, you can pass them as a list of sources. In this case, the different results will be merged into one single output. | ||
Remember that the merge will be done sequentially, so the order of the given list is important. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 The order of the list is significant either way, right? As in, the order determines the order of pages in the merged document. What I meant with "sequentially" in my original comment is that the processing is not concurrent, i.e. when you print n
pages and printing a single page takes t
time, you would expect a total runtime of >= nt
(regardless of the number of sessions in Chrome)
Remember that the merge will be done sequentially, so the order of the given list is important. | |
Please note that processing of individual inputs is not concurrent, so total runtime will scale linearly with the number of inputs. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I missunderstood it then!
Time of printing is also something to be aware of, true!
Let’s see if what @andreasknoepfle said about concurrent printing can become real 💪
setup do | ||
start_supervised!(ChromicPDF) | ||
:ok | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You probably wanted this describe
section to contain the tests above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First I started in here but moved them into the other describe!
Sorry I forgot to delete this 😅
In order to be able to merge multiple PDF files, we have added this Ghostscript command that can be called using
ChromicPDF.merge/2
.This new function accepts a list of paths and an output option that can be added or not depending on the desired output (same as
print_to_pdf/2
output option).