简体   繁体   English

将 PDF 转换为 PNG Node.JS

[英]Convert PDF to PNG Node.JS

I'm building a Node.js application to convert PDF to PNGs and display on the user page.我正在构建一个 Node.js 应用程序来将 PDF 转换为 PNG 并显示在用户页面上。

The app will work like this:该应用程序将像这样工作:

  1. User uploads a PDF to the server用户将 PDF 上传到服务器
  2. Server converts the PDFs pages to individual PNGs服务器将 PDF 页面转换为单独的 PNG
  3. Display PNGs on the User page在用户页面上显示 PNG

I found a great package called Node ImageMagick https://github.com/rsms/node-imagemagick but Its not a perfect fit.我找到了一个名为 Node ImageMagick https://github.com/rsms/node-imagemagick的很棒的包,但它并不完美。

Some things like -monitor flag from ImageMagick doesn't work but doesn't work on vanilla node.js as well: ImageMagick 的-monitor标志之类的一些东西不起作用,但在 vanilla node.js 上也不起作用:

var exec = require('child_process').exec;

exec('convert -monitor myFile.pdf myFile.png', function(error, stdout, stderr) {
    console.log('converting is done');
});

The thing I want to achieve is that the converting function to return the name of the files converted like: myFile-0.png, myFile-1.png.我想要实现的是转换函数返回转换后的文件的名称,如:myFile-0.png、myFile-1.png。

The solution I wanted to implement was to make a directory with the name of the PDF and convert the PNGs there like:我想要实现的解决方案是创建一个带有 PDF 名称的目录,然后将 PNG 转换为:

   exec('convert myFile.pdf myFile/myFile.png', function(error, stdout, stderr) {
        console.log('converting is done');
    });

Then read the content of that directory and send to the user the names of files and the paths.然后读取该目录的内容并将文件名和路径发送给用户。

Is this a good solution?这是一个很好的解决方案吗?

Can someone explain me how to achieve this goal?有人可以解释我如何实现这个目标吗?

Updated Answer更新答案

I think it all comes out at the end because Ghostscript is actually doing the work on behalf of IM.我认为这一切都在最后出现,因为 Ghostscript 实际上是代表 IM 做这项工作的。 You may need to check how many pages there are in your PDF and then do a loop if you want fine-grained control.如果您想要细粒度控制,您可能需要检查 PDF 中有多少页,然后执行循环。

# Get number of pages in "a.pdf"
pages=$(identify a.pdf | wc -l)

# Alternative, faster way of getting number of pages - pdfinfo is part of Poppler package
pages=$(pdfinfo a.pdf | grep "^Pages")

for all pages 0 .. (pages-1)
   convert a.pdf[$page] page-${page}.png
done

Original Answer原答案

Not sure I 100% understand what you want, but I think it is something like this...不确定我 100% 明白你想要什么,但我认为它是这样的......

Say you have a 20-page PDF, called input.pdf .假设您有一个 20 页的 PDF,名为input.pdf If you print the scene number , whose escape sequence is %s , like this如果您打印scene number ,其转义序列为%s ,如下所示

convert input.pdf -format "%s\n" -write info: z%d.png

you will get this:你会得到这个:

Output输出

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

and these files:和这些文件:

ls z*
z0.png  z10.png z12.png z14.png z16.png z18.png z2.png  z4.png  z6.png  z8.png
z1.png  z11.png z13.png z15.png z17.png z19.png z3.png  z5.png  z7.png  z9.png

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM