Coder Social home page Coder Social logo

ajaxray / merge2pdf Goto Github PK

View Code? Open in Web Editor NEW
76.0 6.0 13.0 98 KB

Merge Image and PDF files (optionally with selective pages) with lossless quality

License: GNU Affero General Public License v3.0

Go 97.01% Shell 2.99%
pdf unidoc multiple-pdfs go golang pdf-merge image-merge

merge2pdf's Introduction

merge2pdf - Merge Image and PDF files into PDF

Simplest command line tool for merging Images and PDFs. It will not convert PDF pages (with texts, images, forms) into flat image, everything will remain as is.

merge2pdf - simplest tool for merging image and PDF files

Highlights -

  • Very simple and easy to use
  • Lossless quality by default. Option to adjust JPEG quality.
  • Can be merged only selective pages of PDF
  • Supports wide range of image formats
  • Options to adjust size, margin and scaling of image
  • Supports directory as input (merges all Images/PDFs of directory)
  • Free and open source

Install

It's just a single binary file, no external dependencies. Just download the appropriate version of executable from latest release for your OS. Then rename and give it execute permission.

mv merge2pdf_linux-amd64 merge2pdf  
sudo chmod +x merge2pdf

If you want to install it globally (run from any directory of your system), put it in your systems $PATH directory.

sudo mv merge2pdf /usr/local/bin/merge2pdf

Done!

How to use

# Merge multiple PDFs
merge2pdf output.pdf input1.pdf input2.pdf path/to/other.pdf ...

# Merge 1st page of input1.pdf, full input2.pdf and 2nd, 3rd, 4th page of input3.pdf  
merge2pdf output.pdf input1.pdf~1 input2.pdf input3.pdf~2,3,4

# Merge multiple Images
merge2pdf output.pdf image1.jpg image2.jpg path/to/other.png ...

# Merge all Images/PDFs of one or multiple directory
merge2pdf output.pdf path/to/a/dir path/to/another ...

# Mixing up PDF, PDF Pages, Images and directories
merge2pdf output.pdf doc1.pdf~1,2 image1.jpg path/to/dir path/to/other.pdf ...

Fine tuning Image pages

When adding Images, by default the size for image containing pages will be same to image size with 1 inch margin on each side. But you may set custom margins and resize to standard Print size.

# Set image page size to A4. Other Options are A3, Legal and Letter
merge2pdf -s A4 output.pdf image1.jpg image2.jpg

# Set image page size and margin (left, right, top, bottom).
merge2pdf -s A3 -m .5,.5,1,1 output.pdf image1.jpg image2.jpg
# margin can be set without size

# Scale images to page width, with aspect ratio
# To scale to height, use --scale-height
merge2pdf -s A3 -m .5,.5,1,1 --scale-width output.pdf image1.jpg image2.jpg

# For JPEG, you can set optimized quality
merge2pdf -m 0,0,0,0 --jpeg-quality=80 output.pdf image1.jpg image2.jpg

Note: When adding images and PDFs together, these options will effect ONLY Image pages.

If your filename contains space or some special characters, then quote the filepaths along with page numbers. For safety, you can quote them always.

merge2pdf output.pdf "With Space.pdf" "without-space.pdf" "with space and pages.pdf~2,3,4"

To see details of options,

merge2pdf -h

Notes

  • --size, --margin, --scale-width, --scale-height will only effect images.
  • PDF pages will inherit size and margin of source PDF
  • Some .tiff files (e,g, compression level 4) may not work properly
  • Directory inputs will be searched for image/PDF files on first level only. Subdirectories will be skipped.

Roadmap

✅ Merge multiple PDFs without loosing quality
✅ Merge multiple PDFs with selective pages
✅ Adding Images
✅ Mixing up Images and PDFs
✅️ Merge all (Image and PDF) files from directory
✅ Option to Resize Images to reduce filesize
◻️ Option to Greyscale Images to reduce filesize
◻️ Option to set files and pages as JSON config to make usages from other app more convenient

Contribute

If you fix a bug or want to add/improve a feature, and it's alligned with the focus (merging with ease) of this tool, I will be glad to accept your PR. :)

Thanks

This tool was made using the beautiful Unidoc library. Thanks and ❤️ to Unidoc.


"This is the Book about which there is no doubt, a guidance for those conscious of Allah" - Al-Quran

merge2pdf's People

Contributors

ajaxray avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

merge2pdf's Issues

Merges pdf files

Hello,

Can I marge 2 pdf files starting from the second page to the end, without type single page.

example : merge2pdf output.pdf input1.pdf input2.pdf~2-To the end

Unidoc License

Hi, we would like to use this tool in a commercial product.
How about unidoc License?
Do we need a commercial license for Unidoc?
Thanks

Improved page selection

The Problem

Currently, this library supports the following format for specifying page numbers of input PDF document:

input-pdf-name.pdf~<page-number>[,<page-number>]*
That means comma-separated page numbers after the file name joined with a ~. For example -

merge2pdf output.pdf input1.pdf~1 input2.pdf input3.pdf~2,3,4

This will merge 1st page of input1.pdf, full input2.pdf and 2nd, 3rd, 4th page of input3.pdf.

So we have to mention every page number specifically. It's fine when the command is being executed from an application. But this is not an easy option in case of running the command manually, especially when we have a large number of pages.

Proposal for improvement

To make it easy to provide page numbers, we can add support for the following formats to specify input pages.

  1. Fixed range of inclusive sequence numbers separated with -.
    Example: 2-7 will mean 2nd to 7th pages
  2. Open-ended range. To indicate the last page, $ will be used as the anchor character.
    Example: 4-$ will mean 4th page to last page
  3. Combining page number formats. All supported formats (including current one) can be used combinedly
    Example: 1,2,5-7,11-$ will include 1,2,5,6,7 and 11 to last page

Notes:

  1. The current page number format is parsed in this function.
  2. Based on #1 by @JohnnyMclain12

Out of memory

I have 100 jpg files (85 MB in total) and when I want to convert them to a pdf via

merge2pdf.exe output.pdf "my path"

I get:

runtime: out of memory: cannot allocate 7274496-byte block (1320812544 in use)
fatal error: out of memory

runtime stack:
runtime.throw(0x7bade2, 0xd)
        /usr/local/go/src/runtime/panic.go:596 +0x7c
runtime.largeAlloc(0x6f0000, 0x446700, 0x51404000)
        /usr/local/go/src/runtime/malloc.go:797 +0xdb
runtime.mallocgc.func1()
        /usr/local/go/src/runtime/malloc.go:690 +0x31
runtime.systemstack(0x10ee2000)
        /usr/local/go/src/runtime/asm_386.s:337 +0x5e
runtime.mstart()
        /usr/local/go/src/runtime/proc.go:1132

goroutine 1 [running]:
runtime.systemstack_switch()
        /usr/local/go/src/runtime/asm_386.s:291 fp=0x11013ba8 sp=0x11013ba4
runtime.mallocgc(0x6f0000, 0x0, 0x1d0000, 0x51404000)
        /usr/local/go/src/runtime/malloc.go:691 +0x6c7 fp=0x11013bf8 sp=0x11013ba8
runtime.growslice(0x7659c0, 0x50e78000, 0x58bfff, 0x58c000, 0x58c002, 0x51404000, 0x1d0000, 0x244000)
        /usr/local/go/src/runtime/slice.go:140 +0x1c7 fp=0x11013c44 sp=0x11013bf8
github.com/unidoc/unidoc/pdf/model.DefaultImageHandler.NewImageFromGoImage(0x94b7a0, 0x6095e3c0, 0x94b7a0, 0x6095e3c0, 0x7b1b4f)
        /Users/ajaxray/go/src/github.com/unidoc/unidoc/pdf/model/image.go:247 +0x344 fp=0x11013d18 sp=0x11013c44
github.com/unidoc/unidoc/pdf/model.DefaultImageHandler.Read(0x948c40, 0x593fcf60, 0x20, 0x780760, 0x1)
        /Users/ajaxray/go/src/github.com/unidoc/unidoc/pdf/model/image.go:282 +0x10f fp=0x11013d4c sp=0x11013d18
github.com/unidoc/unidoc/pdf/model.(*DefaultImageHandler).Read(0x9854a0, 0x948c40, 0x593fcf60, 0x36f328c0, 0x0, 0x0)
        <autogenerated>:313 +0x46 fp=0x11013d68 sp=0x11013d4c
github.com/unidoc/unidoc/pdf/creator.NewImageFromData(0x4c794000, 0xcbab0, 0xcbcb0, 0xcbab0, 0xcbcb0, 0x0)
        /Users/ajaxray/go/src/github.com/unidoc/unidoc/pdf/creator/image.go:78 +0x88 fp=0x11013da0 sp=0x11013d68
github.com/unidoc/unidoc/pdf/creator.NewImageFromFile(0x1106c550, 0x46, 0x755940, 0x605f8f70, 0x10f02200)
        /Users/ajaxray/go/src/github.com/unidoc/unidoc/pdf/creator/image.go:94 +0x55 fp=0x11013dc0 sp=0x11013da0
main.createImage(0x1106c550, 0x46, 0x7b204d, 0x5, 0x7b6f91, 0xa, 0x1106c592, 0x4, 0x985624, 0x0, ...)
        /Users/ajaxray/go/src/github.com/ajaxray/merge2pdf/img.go:60 +0x181 fp=0x11013dfc sp=0x11013dc0
main.addImage(0x1106c550, 0x46, 0x7b204d, 0x5, 0x7b6f91, 0xa, 0x1106c592, 0x4, 0x985624, 0x0, ...)
        /Users/ajaxray/go/src/github.com/ajaxray/merge2pdf/img.go:31 +0x31 fp=0x11013e40 sp=0x11013dfc
main.ImgSource.MergeTo(0x1106c550, 0x46, 0x7b204d, 0x5, 0x7b6f91, 0xa, 0x1106c592, 0x4, 0x985624, 0x0, ...)
        /Users/ajaxray/go/src/github.com/ajaxray/merge2pdf/img.go:27 +0xa9 fp=0x11013e84 sp=0x11013e40
main.(*ImgSource).MergeTo(0x1104b2c0, 0x10f164e0, 0x0, 0x0)
        <autogenerated>:3 +0x46 fp=0x11013ec0 sp=0x11013e84
main.DirSource.MergeTo(0x10f08080, 0x30, 0x1101d000, 0x112, 0x200, 0x10f164e0, 0x10f164e0, 0x11001c00)
        /Users/ajaxray/go/src/github.com/ajaxray/merge2pdf/dir.go:21 +0xc5 fp=0x11013ef4 sp=0x11013ec0
main.(*DirSource).MergeTo(0x110152e0, 0x10f164e0, 0x949800, 0x110152e0)
        <autogenerated>:2 +0x46 fp=0x11013f18 sp=0x11013ef4
main.main()
        /Users/ajaxray/go/src/github.com/ajaxray/merge2pdf/main.go:63 +0xe7 fp=0x11013fc8 sp=0x11013f18
runtime.main()
        /usr/local/go/src/runtime/proc.go:185 +0x1d6 fp=0x11013ff0 sp=0x11013fc8
runtime.goexit()
        /usr/local/go/src/runtime/asm_386.s:1629 +0x1 fp=0x11013ff4 sp=0x11013ff0

I'm using Windows 10 and have more than 10 GB of unallocated RAM.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.