No matter it is in life or business, the ability to convert images to PDF and vice versa is an essential skill. Whether you want to maintain the quality of a photo in PDF format or you need to extract images from PDF documents for reuse, Python will accomplish these tasks efficiently.
This guide will demonstrate how to save a photo as PDF and vice versa in Python, including step-by-step instructions and some code examples. Read to learn more!
Prepare for Task
To save pictures as PDFs in Python, we need to use Spire.PDF for Python. It is a comprehensive Python PDF library that can perform nearly all operations related to PDF files, such as format conversion, adding and deleting PDF pages, extracting text from PDFs, and removing annotations. Of course, it also includes the functionality to save a photo as a PDF and vice versa, which will be introduced in this article.
To install it from PyPI, use the following pip command:
pip install Spire.PDF
How to Save a Photo as PDF in Python without Effort
There are many occasions when you need to save a picture as a PDF. For example, when sharing and printing files, the PDF format preserves image quality and layout better. Or, if you need to archive the images, PDF files are easier to search and manage.
This part will introduce how to turn a photo into a PDF and provide a code example to help you better understand the process.
Steps to get PDFs from images:
Create an object of the PdfDocument class.
Set the page margins.
Load the image to be converted to PDF from the disk.
Get the width and height of the image using PhysicalDimension.Width and PhysicalDimension.Height properties.
Add a page that has the same size as the image to the PDF document.
Place the image on the page with Canvas.DrawImage() method.
Save the resulting document using PdfDocument.SaveToFile() method, then release the resources.
Here is a code example of saving a photo as a PDF:
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument object
doc = PdfDocument()
# Set the page margins to 0
doc.PageSettings.SetMargins(0.0)
# Load the image from file
image = PdfImage.FromFile("image.jpg")
# Get the image width and height
width = image.PhysicalDimension.Width
height = image.PhysicalDimension.Height
# Add a page that has the same size as the image
page = doc.Pages.Add(SizeF(width, height))
# Draw image at (0, 0) of the page
page.Canvas.DrawImage(image, 0.0, 0.0, width, height)
# Save to file
doc.SaveToFile("Image2PDF.pdf", FileFormat.PDF)
# Release resources
doc.Dispose()
How to Save a PDF as a Photo with Python: Specified Pages
Having explored how to save a photo as a PDF, it is important to recognize that there are times when you may want to extract content from a PDF. And that requires saving PDFs as images.
In this chapter, we will delve into the methods for how to save a PDF page as a picture, ensuring you can efficiently access the information you need.
Steps to save a PDF page as a photo:
Instantiate a PdfDocument object and open the PDF to be transformed from the file with PdfDocument.LoadFromFile() method.
Convert the PDF page to images.
Store the image in the file path using imageS.Save() method, then release the resource.
Below is a code example of saving the first page to an image:
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument object
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("drylab.pdf")
# Convert the first page to an image
with pdf.SaveAsImage(0) as imageS:
# Save the image as a JPG file
imageS.Save("Page2Image.jpg")
# Release resources
pdf.Close()
How to Save a PDF File as Photos with Python: All Pages
Converting certain PDF pages as photos is a common task, but sometimes you need to save all PDF pages into photos, especially when creating an image gallery, facilitating easier sharing, and so on.
The steps to save each PDF page as a picture are similar to those for saving a specified page. However, you need to iterate through all the pages in the PDF beforehand to ensure that each page is successfully converted without any being missed. The steps are just as follows, read on.
Steps to save all PDF pages as photos in Python:
Create a PdfDocument object.
Read the document to be modified from the disk using PdfDocument.LoadFromFile() method.
Loop through all pages in the document.
Save PDF pages as pictures.
Write these images to the disk, and close the PDF document.
Here is a code example of saving each PDF page as a photo:
from spire.pdf.common import *
from spire.pdf import *
# Create a PdfDocument object
pdf = PdfDocument()
# Load a PDF document
pdf.LoadFromFile("drylab.pdf")
# Iterate through all pages in the document
for i in range(pdf.Pages.Count):
# Save each page as an PNG image
fileName = "ToImage-{0:d}.png".format(i)
with pdf.SaveAsImage(i) as imageS:
imageS.Save(fileName)
# Close the document
pdf.Close()
The Conclusion
The blog illustrates how to save a photo as a PDF in Python and vice versa. Every instruction comes with thorough steps and a code example. We believe that after reading this article, you will be able to easily convert between PDF and image formats!