I want to split multi-page PDF file in my application and it should also support converting PDF to PNG images. Ideally, each page of the PDF can be in an image model so that I can get an image of every page.
def generate_png manipulate! do |image, index| image.format = 'png' image.write("#{Rails.root}/public/#{store_dir}/image-#{index}.png") end end
Unfortunately, when i tried to do this, i only got PNG image of the first frame. I don't know what go wrong?
So I want to ask for help. How can I achieve a new entry in the image model of each frame?
This article on XsPDF.com is what you looking for, check out: Split multipage PDF into several images
Answers
In general, when you split a pdf file to image, it is possible to output only the first frame by default. Because if your PDF file is named as file.pdf, then you will get a series of image results. So you are supposed to check whether your program is set to load all converted images.
Agree! The reason for why your program returns a PNG image of the first frame is probably because it set to do so. If you are using a third-party tool, then you should contact the technical personnel to verify the relevant default setting.
In fact, XsPDF converter SDK allows you to customize PDF to images conversion. It supports not only the conversion of entire PDF document, but also the conversion of a single page.
// Create a PDF converter instance by loading a local file PdfImageConverter pdfConverter = new PdfImageConverter("sample.pdf"); for (int i = 0; i < pdfConverter.PageCount; i++) { // Convert pdf to png in customized image size Image pageImage = pdfConverter.PageToImage(i, 500, 800); // Save converted image to png format pageImage.Save("Page " + i + ".png", ImageFormat.Png); }
the above method will create png images from the pdf file and store them in the same directory and the same name of the original pdf file