How to generate a PDF from a series of images?

78

Solution 1

I found a solution which uses ImageMagick, but it took me a while to figure out that I had to use the option -page A4 to generate an A4 formatted PDF. In the end this simple command did the trick for me:

convert *.gif -page A4 book.pdf

Troubleshooting

While googling for an answer I realized that other people also have tried this, and encountered problems which I never saw. These hints may be helpful to some of you.

Images of different sizes: If your images do not all have the same size, have a look at this YouTube video: “How to convert multiple images to A4 pdf using imagemagick”. The guy in the video uses the command (which he states produces A4 output):

convert *.jpg -resize 595x842 -gravity center -background white \
    -extent 595x842 resultimage.pdf

Postamble

This question has been brought up on stackoverflow, but closed as off topic (“How can I convert a series of images to a PDF from the command line on linux?”). As this gave me a hint, but not the full solution I'm creating (and answering) this question here, in the hope that someone else will find it useful, too.

Solution 2

Use img2pdf:

img2pdf --pagesize A4 file1.jpg file2.jpg > book.pdf

From its description:

Losslessly convert raster images to PDF without re-encoding JPEG and JPEG2000 images. This leads to a lossless conversion of JPEG and JPEG2000 images with the only added file size coming from the PDF container itself.

ImageMagick on the other hand will decode and re-encode the JPEG data, resulting in generation loss and performance 10–100 worse than img2pdf.

Share:
78

Related videos on Youtube

Chainsaw
Author by

Chainsaw

Updated on September 18, 2022

Comments

  • Chainsaw
    Chainsaw over 1 year

    everyone. What I want is get field of a related model by serializer. I have 2 models:

    class Question(models.Model):
        question_text = models.CharField(max_length=200)
        def __str__(self):
            return self.question_text    
    
    
    
    class Test(models.Model):
        test_name = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
        question = models.ManyToManyField(Question, related_name='tests')
        def __str__(self):
            return self.test_name
    

    Why I've used ManyToManyField? Because I've followed here: https://medium.com/django-rest/lets-build-a-basic-product-review-backend-with-drf-part-1-652dd9b95485

    Now I can do smth like: enter image description here

    But I want get question_text in response. What I tried:

    class TestSerializer(serializers.HyperlinkedModelSerializer):
        question_text = serializers.CharField(read_only=True, source="question.question_text")
        class Meta:
            model = Test
            fields = ['pk', 'test_name', 'pub_date', 'question_text']
            expandable_fields = {
     'question': (QuestionSerializer, {'many': True})
    }
    

    But it returned:

    enter image description here

    I understand, that problem might be in DB relations, but I can't get it. Thanks in advance!

  • I say Reinstate Monica
    I say Reinstate Monica over 5 years
    Welcome to Super User! Please read How to recommend software for minimum required information and other suggestions on how to recommend software on Super User. To keep your answer useful even if included link(s) breaks please edit these details into your answer. Without knowing what convert is this answer is not useful.
  • vstepaniuk
    vstepaniuk about 4 years
    what about PNG?
  • vstepaniuk
    vstepaniuk about 4 years
    Found in man img2pdf: > Losslessly convert raster images to PDF without re-encoding PNG, JPEG, and JPEG2000 images. This leads to a lossless conversion of PNG, JPEG and JPEG2000 images with the only added file size coming from the PDF container itself. Other raster graphics formats are losslessly stored using the same encoding that PNG uses. Since PDF does not support images with transparency and since img2pdf aims to never be lossy, input images with an alpha channel are not supported.