Optimal way of saving .pdf as .png pages #177
-
Hi! Thanks for a fantastic library! What's a recommended way of saving PDF pages as .png files? I have arrived at the following code, it's working, but it seems like reading pdf pages in a loop is only sub-optimal: I need to read the PDF file N+1 times, where N is the number of pages in PDF. After all, each time I call I'm not familiar with vips API, hence the question. Mix.install [:vix]
[path] = System.argv()
{:ok, img} = Vix.Vips.Image.new_from_file(path)
{:ok, pages} = Vix.Vips.Image.header_value(img, "n-pages")
Enum.each(1..pages, fn page ->
{:ok, {img, _flags}} = Vix.Vips.Operation.pdfload(path, dpi: 300, page: page)
:ok = Vix.Vips.Operation.pngsave(img, "/tmp/page-#{page}.png")
end) For context, the above |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @gmile! Thanks for trying Vix. libvips lazy loads wherever it is possible, so it does not loads the whole image when you ask for a specific page with If you see abnormal memory usage then please share more details. |
Beta Was this translation helpful? Give feedback.
Hi @gmile! Thanks for trying Vix.
libvips lazy loads wherever it is possible, so it does not loads the whole image when you ask for a specific page with
page
, andn
(# pages) with value 1 (default). So it is already optimal in that sense. If you have large number of pages then you can write multiple pages concurrently usingTask.async_stream
at the cost of more memory.If you see abnormal memory usage then please share more details.