Phoenix LiveView pdf viewer

Hey All,

Building a Phoenix LiveView app that allows you to upload a file (currently only markdown supported) and advance through the pages one at a time, and I would like it to support pdf files. I have been looking for a package that can do this, but I can’t find one.

I’d love to be able to provide a pdf, and a page number and render that page of the pdf in my LiveView page.

Is there anything out there like this for Elixir/Phoenix?

1 Like

You will need hooks and pdf.js, or no hooks but an iframe.

1 Like

You probably won’t find an existing solution but it should be easy to build one yourself:

1 Like

In the past I’ve used the command line tool pdfimages that’s included in poppler and had some success with it. For example, you can shell out to pdfimages -list <filename> to get a list of pages with some information about them, and you can convert the PDF into a series of images that you can render in LiveView.

1 Like

pdftocairo also works nicely (it renders fonts pretty well, being based on the poppler pdf rendering lib, and can also convert to SVG). For example you generate a preview image of the first page with pdftocairo -png -singlefile -scale-to #{max_size} #{original_path} #{output_path}

1 Like

I’ve just experimented with the SVG support. There’s an issue with multipage PDFs in that it generates multi-page SVGs which unfortunately doesn’t seem supported by browsers. You can do one page at a time though with pdfseparate input.pdf ouput-%d.pdf (that command also comes with poppler) and then pdftocairo -svg on each page.

1 Like