0

I have searching for a way to convert pdf and ppt files to images. I have gone through a lot of websites and links. With the invent of NodeJS, I was hoping for something simple.

I found this one: convert pptx/ppt/docx/doc to images. It promises a simpler solution to my problem. However, it is written in Meteor, and I think there might be more to it than meets the eye. I want a simple NodeJS way to do this, without the Meteor coating, as I have no previous knowledge of Metor.

meteorhacks:npm

npm-container
Meteor.startup(function () {
    var fs = Npm.require("fs");
    var path = Npm.require('path');
    var sys = Npm.require('sys');
    var exec = Npm.require('child_process').exec;
    var child;

    var dir = "/home/bhavesh";

    fs.watch(dir, {persistent: true}, function (event, fileName) {
        console.log("dfs");
        console.log("Event: " + event);
        console.log(fileName + "\n");
        var ext = path.extname(fileName);
        if ((ext.toString() == ".pptx" || ext.toString() == ".ppt" || ext.toString() == ".docx" || ext.toString() == ".doc") && event == "change") {
            console.log("Powerpoint file");
            ppt_or_doc_to_pdf(dir,fileName);
        }
        if (ext.toString() == ".pdf" && event == "change") {
            console.log("PDF file");
            pdf_to_ppm(dir,fileName);
        }
    });
    function pdf_to_ppm(dir, file) {
        var cmd = "cd " + dir + "; pdftoppm '" + file + "' slide";
        exec(cmd, function (error, stdout, stderr) {
            console.log('stdout: ' + stdout);
            console.log('stderr: ' + stderr);
            if (error !== null) {
                console.log('exec error: ' + error);
            }
            ppm_to_jpg(dir);
        });
    }

    function ppt_or_doc_to_pdf(dir, file) {
        var fileName = file.substring(0, file.indexOf('.'));
        var cmd = "cd " + dir + "; unoconv -f pdf -o '" + fileName + ".pdf' '" + file + "'";
        exec(cmd, function (error, stdout, stderr) {
            console.log('stdout: ' + stdout);
            console.log('stderr: ' + stderr);
            if (error !== null) {
                console.log('exec error: ' + error);
            }
            pdf_to_ppm(dir, fileName + '.pdf');
        });
    }
    function ppm_to_jpg(dir) {
        var cmd = "cd " + dir + "; mogrify -format jpg slide*.ppm";
        exec(cmd, function (error, stdout, stderr) {
            console.log('stdout: ' + stdout);
            console.log('stderr: ' + stderr);
            if (error !== null) {
                console.log('exec error: ' + error);
            }
        });
    }
});

Any help is appreciated...

Thanks..

Clinton Yeboah
  • 215
  • 2
  • 5
  • 1
    I think this code has little to do with meteor. It just starts a number of executables in a shell. Remove the 'Meteor.startup(function () {' line and you are good to go – mvermand Feb 28 '17 at 20:30
  • Thanks.. I am new to JavaScript world, does the code really converts the files or is just changing their extensions? – Clinton Yeboah Feb 28 '17 at 20:32
  • I think it really converts the files, but you will need the "pdftoppm", "unoconv" and "mogrify" executables. – mvermand Feb 28 '17 at 20:41
  • It seems these executables are only available via linux and not specific to node. I'm developing on a linux system, but I'm not sure about the deployment machine. – Clinton Yeboah Feb 28 '17 at 22:08
  • I think you need to know. It will be harder to build it cross-platform. – mvermand Mar 01 '17 at 06:20
  • Even these executables are hard to come by. Does anyone has a better solution out there – Clinton Yeboah Mar 01 '17 at 08:05
  • You could use Apache PDFBox for the PDF to Image conversion: http://stackoverflow.com/questions/23326562/apache-pdfbox-convert-pdf-to-images. PDFBox is a Java implementation, thus cross-platform. That still leaves the ppt/doc to PDF conversion... – mvermand Mar 01 '17 at 09:53
  • maybe this: http://superuser.com/questions/718767/is-there-any-command-line-docx-to-image-converter-available but I think that is on windows (only?) – mvermand Mar 01 '17 at 12:17
  • Why don't you use the Powerpoint "Save as" [object model](https://msdn.microsoft.com/en-us/library/ff746389.aspx)? You can save directly to pdf or jpg and so on. For pdf to pptx I would recommend on-line converter. – Christine Ross Mar 01 '17 at 20:09
  • Thanks guys for your help. I think I should explain my intention for this. I'm working on a website for my university. Teachers will upload their lectures slides which is usually PPT or PDF files. They can be very big. And I need a way to gather them into a newsfeed kind of thing for nice display. Getting all the files will be heavy. I want to show two or three images from the uploaded files instead, and when they click on them, they get the original files. This I think will reduce website load time. Thanks – Clinton Yeboah Mar 02 '17 at 05:09
  • I'm using vuejs for the client and loopback(nodejs) for the backend – Clinton Yeboah Mar 02 '17 at 05:10

0 Answers0