fpdf single output with multiple images
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
add a comment |
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
add a comment |
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
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
python pdf pdf-generation cfpdf
asked Apr 1 '17 at 12:50
wolfmanwolfman
112
112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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))
Works Great! Thank you for sharing!
– Xonshiz
Jul 6 '17 at 16:10
add a comment |
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")
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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))
Works Great! Thank you for sharing!
– Xonshiz
Jul 6 '17 at 16:10
add a comment |
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))
Works Great! Thank you for sharing!
– Xonshiz
Jul 6 '17 at 16:10
add a comment |
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))
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))
answered Apr 3 '17 at 18:44
wolfmanwolfman
112
112
Works Great! Thank you for sharing!
– Xonshiz
Jul 6 '17 at 16:10
add a comment |
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
add a comment |
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")
add a comment |
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")
add a comment |
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")
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")
edited Nov 21 '18 at 10:41
Unheilig
12k165386
12k165386
answered Nov 21 '18 at 10:24
Maximilian SenftlebenMaximilian Senftleben
113
113
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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