How to render a PyPDF.PageObject page to a PIL image in python? - python-imaging-library

can you help me to render a pdf page opened using PyPDF2 into a PIL image in python3? Thank you!!!

I don't think PyPDF2 can render a pdf page. However, you can use PyMuPDF to do this. Here is the tutorial of PyMuPDF.
Here is an example of rendering with PyMuPDF:
import fitz
from PIL import Image
filename = "test.pdf" # name of pdf file you want to render
n = 0 # n is the page number
#render with PyMuPDF
doc = fitz.open(filename)
page = doc.loadPage(n)
pix = page.getPixmap()
#convert to a PIL image
img = Image.frombytes("RGBA", [pix.width, pix.height], pix.samples)

Related

How to manipulate all pages in a multi-page tiff image without converting it into list of single images in Pillow?

I would like to insert a small image to each page in a multi-page tiff using Pillow.
But this fails:
from PIL import Image, ImageSequence
stack = Image.open(r"D:\pillow_temp\stack_example.tif") # opens an rgb multi tiff
insert= Image.open("D:\pillow_temp\insert.tif") # opens a single page rgb multi tiff
for page in ImageSequence.Iterator(stack):
page.paste(insert,(0,0))
stack.save(r"D:\pillow_temp\out_fails.tif", format="TIFF", save_all=True)
Transforming the multi-page tiff to a list and saving one changed image first followed by appending the rest of the images works but doesn't feel proper.
pages= []
for page in ImageSequence.Iterator(stack):
page = page.copy()
page.paste(insert,(0,0))
pages.append(page)
pages[0].save(r"D:\pillow_temp\out_works.tif", format="TIFF", save_all=True, append_images=pages[1:])
Is there a way to manipulate the pages directly in the PIL.TiffImagePlugin.TiffImageFile object and call im.save("filename.tiff", format="TIFF", save_all=True) directly?
All help is appreciated.
https://pillow.readthedocs.io/en/stable/reference/ImageSequence.html?highlight=multi%20tiff%20iterator#the-iterator-class
https://pillow.readthedocs.io/en/stable/releasenotes/3.4.0.html?highlight=multitiff%20image%20save#save-multiple-frame-tiff

font registration for flying saucer with ITextRenderer

I am using Flying Saucer to generate pdf from html using iText-2.1.7.
For each document I print, I do:
ITextRenderer renderer = new ITextRenderer();
for each font I use:
renderer.getFontResolver().addFont(font.getPath(),font.getName(), BaseFont.CP1252, BaseFont.EMBEDDED, null);
and then the rest of the code to create the pdf:
renderer.getSharedContext().setReplacedElementFactory(...)
renderer.getSharedContext().setUserAgentCallback(...);
renderer.setDocument(xhtmlDom,null);
renderer.layout();
renderer.createPDF(byteArrayOutputStream);
So, for each document, I register all the fonts again and again...
Is this strictly necessary? Is there any font cache service I can use, or something similar?
Thank you very much!

iTextSharp convert to .pdf - change MIME Type

Resolved:
This had nothing to do with iTextSharp. The culprit is XMPIE and how it renders pdfs.
I'm using iTextSharp to convert .pngs to pdfs.
Here's a snippet of code:
var image = iTextSharp.text.Image.GetInstance(imageBytes);
document.Add(image);
My question is: does this actually convert the .png to a .pdf? Does it change the Mime type?
I am trying to use the converted image in XMPIE, which does not support .pngs. The "conversion" seems to work -- meaning that I get a pdf that has my image on it. But when I try to display that image in xmpie it displays all blurred out and grey. The "conversion" works with .jpegs but not .pngs.
Is there another way to convert an image (.png) to a .pdf. My code just seems to add the image to the .pdf and doesn't actually change the mime type.

itext7 pdf to image

I am using iText7(java) and am looking for a way to convert a pdf page to image.
In older iText versions you could do this :
PdfImportedPage page = writer.getImportedPage(reader, 1);
Image image = Image.getInstance(page);
But iText7 does not have PdfImportedPage .
My use case, I have a one page pdf file. I need to add a table and resize the pdf contents to fit a single page. In old iText I would create a page , add table, convert existing pdf page to image, resize image and add that resized image to new page. Is there a new way to do this in iText7.
Thanks to Bruno's answer I got this working with following code :
PdfPage origPage = readerDoc.getPage(1);
Rectangle rect = origPage.getPageSize();
Document document = new Document(writerDoc);
Table wrapperTable = new Table(1);
Table containerTable = new Table(new float[]{0.5f,0.5f});
containerTable.setWidthPercent(100);
containerTable.addCell( "col1");
containerTable.addCell("col2");
PdfFormXObject pageCopy = origPage.copyAsFormXObject(writerDoc);
Image image = new Image(pageCopy);
image.setBorder(Border.NO_BORDER);
image.setAutoScale(true);
image.setHeight(rect.getHeight()-250);
wrapperTable.addCell(new Cell().add(containerTable).setBorder(Border.NO_BORDER));
wrapperTable.addCell(new Cell().add(image).setBorder(Border.NO_BORDER));
document.add(wrapperTable);
document.close();
readerDoc.close();
Please read the official documentation for iText 7, more specifically Chapter 6: Reusing existing PDF documents
In PDF, there's the concept of Form XObjects. A Form XObject is a piece of PDF content that is stored outside the content stream of a page, hence XObject which stands for eXternal Object. The use of the word Form in Form XObject could be confusing, because people might be thinking of a form as in a fillable form with fields. To avoid that confusing, we introduced the term PdfTemplate in iText 5.
The class PdfImportedPage you refer to was a subclass of PdfTemplate: it was a piece of PDF syntax that could be reused in another page. Over the years, we noticed that people also got confused by the word PdfTemplate.
In iText 7, we returned to the basics. When talking about a Form XObject, we use the class PdfFormXObject. When talking about a page in a PDF file, we use the class PdfPage.
This is how we get a PdfPage from an existing document:
PdfDocument origPdf = new PdfDocument(new PdfReader(src));
PdfPage origPage = origPdf.getPage(1);
This is how we use that page in a new document:
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf);
If you want to use that pageCopy as an Image, just create it like this:
Image image = new Image(pageCopy);

Creating a PDF image in iText

I'm trying to add an image that I have on the filesystem as a pdf into a pdf that I'm creating on the fly.
I tried to use the Image class, but it seems that it does not work with PDFs (only JPEG, PNG or GIF). How can I create an element from an existing PDF, so that I can place it in my new PDF?
Please download chapter 6 of my book and read all about the class PdfImportedPage.
In the most basic example, you'd create a PdfReader instance and import the page into the PdfWriter instance, from which point on you can use the PdfImportedPage instance, either directly, or wrapped inside an Image object:
PdfReader reader = new PdfReader(existingPdf);
PdfImportedPage page = writer.getImportedPage(reader, i);
Image img = Image.getInstance(page);
reader.close();