fpdf single output with multiple images












0















How do you output all images in a directory to the same pdf output using fpdf. I can get the last file in the folder to output as a pdf or output multiple pdfs for each image, but not all images in the same pdf like a catalog. I'm sure it is the for loop but can't sort it out properly.



from fpdf import FPDF
from PIL import Image
import glob
import os


# set here
image_directory = '/home/User/Desktop/images/'
extensions = ('*.jpg','*.jpeg','*.png','*.gif')


# set 0 if you want to fit pdf to image
# unit : pt
margin = 10

imagelist=
for ext in extensions:
imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

pdf = FPDF(unit="pt", format=[width + 2*margin, height + 2*margin])
pdf.add_page()
cover = Image.open(imagePath)
width, height = cover.size

for imagePath in imagelist:
pdf.image(imagePath, margin, margin)
destination = os.path.splitext(imagePath)[0]
pdf.output(destination + ".pdf", "F")









share|improve this question



























    0















    How do you output all images in a directory to the same pdf output using fpdf. I can get the last file in the folder to output as a pdf or output multiple pdfs for each image, but not all images in the same pdf like a catalog. I'm sure it is the for loop but can't sort it out properly.



    from fpdf import FPDF
    from PIL import Image
    import glob
    import os


    # set here
    image_directory = '/home/User/Desktop/images/'
    extensions = ('*.jpg','*.jpeg','*.png','*.gif')


    # set 0 if you want to fit pdf to image
    # unit : pt
    margin = 10

    imagelist=
    for ext in extensions:
    imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

    pdf = FPDF(unit="pt", format=[width + 2*margin, height + 2*margin])
    pdf.add_page()
    cover = Image.open(imagePath)
    width, height = cover.size

    for imagePath in imagelist:
    pdf.image(imagePath, margin, margin)
    destination = os.path.splitext(imagePath)[0]
    pdf.output(destination + ".pdf", "F")









    share|improve this question

























      0












      0








      0








      How do you output all images in a directory to the same pdf output using fpdf. I can get the last file in the folder to output as a pdf or output multiple pdfs for each image, but not all images in the same pdf like a catalog. I'm sure it is the for loop but can't sort it out properly.



      from fpdf import FPDF
      from PIL import Image
      import glob
      import os


      # set here
      image_directory = '/home/User/Desktop/images/'
      extensions = ('*.jpg','*.jpeg','*.png','*.gif')


      # set 0 if you want to fit pdf to image
      # unit : pt
      margin = 10

      imagelist=
      for ext in extensions:
      imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

      pdf = FPDF(unit="pt", format=[width + 2*margin, height + 2*margin])
      pdf.add_page()
      cover = Image.open(imagePath)
      width, height = cover.size

      for imagePath in imagelist:
      pdf.image(imagePath, margin, margin)
      destination = os.path.splitext(imagePath)[0]
      pdf.output(destination + ".pdf", "F")









      share|improve this question














      How do you output all images in a directory to the same pdf output using fpdf. I can get the last file in the folder to output as a pdf or output multiple pdfs for each image, but not all images in the same pdf like a catalog. I'm sure it is the for loop but can't sort it out properly.



      from fpdf import FPDF
      from PIL import Image
      import glob
      import os


      # set here
      image_directory = '/home/User/Desktop/images/'
      extensions = ('*.jpg','*.jpeg','*.png','*.gif')


      # set 0 if you want to fit pdf to image
      # unit : pt
      margin = 10

      imagelist=
      for ext in extensions:
      imagelist.extend(glob.glob(os.path.join(image_directory,ext)))

      pdf = FPDF(unit="pt", format=[width + 2*margin, height + 2*margin])
      pdf.add_page()
      cover = Image.open(imagePath)
      width, height = cover.size

      for imagePath in imagelist:
      pdf.image(imagePath, margin, margin)
      destination = os.path.splitext(imagePath)[0]
      pdf.output(destination + ".pdf", "F")






      python pdf pdf-generation cfpdf






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Apr 1 '17 at 12:50









      wolfmanwolfman

      112




      112
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Still not sure why fpdf was not grouping the iteration of image files as a single PDF output but img2pdf accepted a list of file names worked very nicely.



          import img2pdf

          imagefiles = ["test1.jpg", "test2.png"]

          with open("images.pdf","wb") as f:
          f.write(img2pdf.convert(imagefiles))





          share|improve this answer
























          • Works Great! Thank you for sharing!

            – Xonshiz
            Jul 6 '17 at 16:10



















          0














          You will have to start a new page for every picture in the beginning of the for loop and end with writing everything to the PDF.



          pdf = FPDF()
          imagefiles = ["test1.png","test2.png"]
          for one_image in imagefiles:
          pdf.add_page()
          pdf.image(one_image,x,y,w,h)
          pdf.output("out.pdf")





          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43157095%2ffpdf-single-output-with-multiple-images%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Still not sure why fpdf was not grouping the iteration of image files as a single PDF output but img2pdf accepted a list of file names worked very nicely.



            import img2pdf

            imagefiles = ["test1.jpg", "test2.png"]

            with open("images.pdf","wb") as f:
            f.write(img2pdf.convert(imagefiles))





            share|improve this answer
























            • Works Great! Thank you for sharing!

              – Xonshiz
              Jul 6 '17 at 16:10
















            1














            Still not sure why fpdf was not grouping the iteration of image files as a single PDF output but img2pdf accepted a list of file names worked very nicely.



            import img2pdf

            imagefiles = ["test1.jpg", "test2.png"]

            with open("images.pdf","wb") as f:
            f.write(img2pdf.convert(imagefiles))





            share|improve this answer
























            • Works Great! Thank you for sharing!

              – Xonshiz
              Jul 6 '17 at 16:10














            1












            1








            1







            Still not sure why fpdf was not grouping the iteration of image files as a single PDF output but img2pdf accepted a list of file names worked very nicely.



            import img2pdf

            imagefiles = ["test1.jpg", "test2.png"]

            with open("images.pdf","wb") as f:
            f.write(img2pdf.convert(imagefiles))





            share|improve this answer













            Still not sure why fpdf was not grouping the iteration of image files as a single PDF output but img2pdf accepted a list of file names worked very nicely.



            import img2pdf

            imagefiles = ["test1.jpg", "test2.png"]

            with open("images.pdf","wb") as f:
            f.write(img2pdf.convert(imagefiles))






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 3 '17 at 18:44









            wolfmanwolfman

            112




            112













            • Works Great! Thank you for sharing!

              – Xonshiz
              Jul 6 '17 at 16:10



















            • Works Great! Thank you for sharing!

              – Xonshiz
              Jul 6 '17 at 16:10

















            Works Great! Thank you for sharing!

            – Xonshiz
            Jul 6 '17 at 16:10





            Works Great! Thank you for sharing!

            – Xonshiz
            Jul 6 '17 at 16:10













            0














            You will have to start a new page for every picture in the beginning of the for loop and end with writing everything to the PDF.



            pdf = FPDF()
            imagefiles = ["test1.png","test2.png"]
            for one_image in imagefiles:
            pdf.add_page()
            pdf.image(one_image,x,y,w,h)
            pdf.output("out.pdf")





            share|improve this answer






























              0














              You will have to start a new page for every picture in the beginning of the for loop and end with writing everything to the PDF.



              pdf = FPDF()
              imagefiles = ["test1.png","test2.png"]
              for one_image in imagefiles:
              pdf.add_page()
              pdf.image(one_image,x,y,w,h)
              pdf.output("out.pdf")





              share|improve this answer




























                0












                0








                0







                You will have to start a new page for every picture in the beginning of the for loop and end with writing everything to the PDF.



                pdf = FPDF()
                imagefiles = ["test1.png","test2.png"]
                for one_image in imagefiles:
                pdf.add_page()
                pdf.image(one_image,x,y,w,h)
                pdf.output("out.pdf")





                share|improve this answer















                You will have to start a new page for every picture in the beginning of the for loop and end with writing everything to the PDF.



                pdf = FPDF()
                imagefiles = ["test1.png","test2.png"]
                for one_image in imagefiles:
                pdf.add_page()
                pdf.image(one_image,x,y,w,h)
                pdf.output("out.pdf")






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 21 '18 at 10:41









                Unheilig

                12k165386




                12k165386










                answered Nov 21 '18 at 10:24









                Maximilian SenftlebenMaximilian Senftleben

                113




                113






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43157095%2ffpdf-single-output-with-multiple-images%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                    Alcedinidae

                    Can an atomic nucleus contain both particles and antiparticles? [duplicate]