Thursday, May 26, 2011

Convert PDF to SWF


You can use swftools (http://www.swftools.org/) to convert pdf to swf.
Installer available for Windows and Linux. http://www.swftools.org/download.html
Here is some sample example of how to use swftools - http://www.quiss.org/swftools/pdf2swf_usage.html

After conversion of pdf to swf you can use flash player to view in a webpage (http://flexpaper.devaldi.com/). Flexpaper is free to use.

Here sample code snippet -
private static String Pdf2SwfExecutablePath="C:\\Program Files\\SWFTools\\pdf2swf.exe";
private ByteArrayOutputStream convertPdfToSwf(ByteArrayOutputStream pdfOutput) throws IOException {
        ByteArrayOutputStream swfByte = new ByteArrayOutputStream();

        // Temporary file name
        String destPDF = tmpdir + System.getProperty("file.separator") + new java.util.Date().getTime() + "_out.pdf";
        String destSWF = tmpdir + System.getProperty("file.separator") + new java.util.Date().getTime() + "_out.swf";
        File destPDFFile = new File(destPDF);
        File destSWFFile = new File(destSWF);

        FileOutputStream pdfFile = new FileOutputStream(destPDFFile);
        pdfFile.write(pdfOutput.toByteArray());
        pdfFile.close();

        Runtime r = Runtime.getRuntime();
        Process p = null;
        String cmd = new String("\"" + Pdf2SwfExecutablePath + "\" \"" + destPDF + "\" -o \"" + destSWF + "\" -f -T 9 -t -s storeallcharacters");
        try {
            p = r.exec(cmd);
            SwfConverterProcessWatcher w = new SwfConverterProcessWatcher(p);
            w.start();
        } catch (Exception e) {
            System.out.println("error executing " + cmd);
        }

        FileInputStream swfFile = new FileInputStream(destSWFFile);
        for (int ch; (ch = swfFile.read()) != -1;)
            swfByte.write(ch);
        swfFile.close();
        swfByte.close();

        // Delete temporary files
        destPDFFile.delete();
        destSWFFile.delete();

        return swfByte;
    }

public class SwfConverterProcessWatcher {
    private Process process;
    private Thread end;
    private Thread out;
    private boolean stopped = false;

    public SwfConverterProcessWatcher(Process theProcess) {
        this.process = theProcess;
        end = new Thread() {
            @Override
            public void run() {
                try {
                    process.waitFor();
                } catch (Throwable e) {
                } finally {
                    stopped = true;
                }
            }
        };
        out = new Thread() {
            @Override
            public void run() {
                String read;
                BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
                while (!stopped) {
                    try {
                        read = in.readLine();
                        if (read == null) {
                            break;
                        }
                    } catch (Throwable e) {
                        break;
                    }
                }
            }
        };
    }

    public void start() {
        end.start();
        out.start();
        try {
            end.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

No comments:

Post a Comment