如何将PDF转换为可在web上浏览的图像?

如何将PDF转换为可在web上浏览的图像?,pdf,grails,groovy,Pdf,Grails,Groovy,我需要创建一个在线查看器,将PDF文件转换为可浏览的图像,如。我想在Grails中这样做。Grails有任何插件吗?下载PDFRenderer.jar fie,编写代码如下 downloadedfile = request.getFile('sourceFile'); println "download file->"+downloadedfile File destFile=new File('web-app/source-pdf/'+downloadedfile+'.pdf'); i

我需要创建一个在线查看器,将PDF文件转换为可浏览的图像,如。我想在Grails中这样做。Grails有任何插件吗?

下载PDFRenderer.jar fie,编写代码如下

downloadedfile = request.getFile('sourceFile');
println "download file->"+downloadedfile
File destFile=new File('web-app/source-pdf/'+downloadedfile+'.pdf');

if(destFile.exists()){

     destFile.delete();

}

File file = null;
 try{
   file = new File('web-app/source-pdf/'+downloadedfile+'.pdf');
    downloadedfile.transferTo(file)
     println "file->"+file
 }catch(Exception e){
    System.err.println("File Already Use")

   //out.close();
 }

File imageFile=new File("web-app/pdf-images");

  if(imageFile.isDirectory())
  {
      String[] list=imageFile.list()

     for(int i=0;i<list.length;i++){
       File img=new File("web-app/pdf-images/"+i+".png")
         img.delete()

     }
  }
   //response.setContentType("image/png");
  // response.setHeader("Cache-control", "no-cache");

    RandomAccessFile raf;
    BufferedImage[] img;

    //  response.setContentType("image/png");
      //  response.setHeader("Cache-control", "no-cache");





    file=new File('web-app/source-pdf/'+downloadedfile+'.pdf');

    try {
        raf = new RandomAccessFile(file, "rws");

        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());
        PDFFile pdffile = new PDFFile(buf);
        // draw the first page to an image
        int num=pdffile.getNumPages();
      img=new BufferedImage[num]

        for(int i=0;i<num;i++)
        {
            PDFPage page = pdffile.getPage(i);

            //get the width and height for the doc at the default zoom
            int width=(int)page.getBBox().getWidth();
            int height=(int)page.getBBox().getHeight();

            Rectangle rect = new Rectangle(0,0,width,height);
            int rotation=page.getRotation();
            Rectangle rect1=rect;
            if(rotation==90 || rotation==270)
                rect1=new Rectangle(0,0,(int)rect.height,(int)rect.width);


            //generate the image
           img[i] = (BufferedImage)page.getImage(
                        width,height , //width & height
                        rect1, // clip rect
                        null, // null for the ImageObserver
                        true, // fill background with white
                        true  // block until drawing is done
                );

            ImageIO.write(img[i], "png",new File("web-app/pdf-images/"+i+".png"));
        }

    //  out.close();
    }
    catch (FileNotFoundException e1) {
        System.err.println(e1.getLocalizedMessage());
    } catch (IOException e) {
        System.err.println(e.getLocalizedMessage());
    }
    file = null;
    render(view:'save',model:[images:img])
downloadedfile=request.getFile('sourceFile');
println“下载文件->”+下载文件
File destFile=新文件('web-app/source pdf/'+downloaddedfile+'.pdf');
如果(destFile.exists()){
destFile.delete();
}
File=null;
试一试{
file=新文件('web-app/source pdf/'+downloaddedfile+'.pdf');
downloadedfile.transferTo(文件)
println“文件->”+文件
}捕获(例外e){
System.err.println(“文件已在使用”)
//out.close();
}
File imageFile=新文件(“web app/pdf图像”);
if(imageFile.isDirectory())
{
String[]list=imageFile.list()

对于(inti=0;i,可以通过下载PDFRenderer.jarfie来实现,编写代码如下

downloadedfile = request.getFile('sourceFile');
println "download file->"+downloadedfile
File destFile=new File('web-app/source-pdf/'+downloadedfile+'.pdf');

if(destFile.exists()){

     destFile.delete();

}

File file = null;
 try{
   file = new File('web-app/source-pdf/'+downloadedfile+'.pdf');
    downloadedfile.transferTo(file)
     println "file->"+file
 }catch(Exception e){
    System.err.println("File Already Use")

   //out.close();
 }

File imageFile=new File("web-app/pdf-images");

  if(imageFile.isDirectory())
  {
      String[] list=imageFile.list()

     for(int i=0;i<list.length;i++){
       File img=new File("web-app/pdf-images/"+i+".png")
         img.delete()

     }
  }
   //response.setContentType("image/png");
  // response.setHeader("Cache-control", "no-cache");

    RandomAccessFile raf;
    BufferedImage[] img;

    //  response.setContentType("image/png");
      //  response.setHeader("Cache-control", "no-cache");





    file=new File('web-app/source-pdf/'+downloadedfile+'.pdf');

    try {
        raf = new RandomAccessFile(file, "rws");

        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());
        PDFFile pdffile = new PDFFile(buf);
        // draw the first page to an image
        int num=pdffile.getNumPages();
      img=new BufferedImage[num]

        for(int i=0;i<num;i++)
        {
            PDFPage page = pdffile.getPage(i);

            //get the width and height for the doc at the default zoom
            int width=(int)page.getBBox().getWidth();
            int height=(int)page.getBBox().getHeight();

            Rectangle rect = new Rectangle(0,0,width,height);
            int rotation=page.getRotation();
            Rectangle rect1=rect;
            if(rotation==90 || rotation==270)
                rect1=new Rectangle(0,0,(int)rect.height,(int)rect.width);


            //generate the image
           img[i] = (BufferedImage)page.getImage(
                        width,height , //width & height
                        rect1, // clip rect
                        null, // null for the ImageObserver
                        true, // fill background with white
                        true  // block until drawing is done
                );

            ImageIO.write(img[i], "png",new File("web-app/pdf-images/"+i+".png"));
        }

    //  out.close();
    }
    catch (FileNotFoundException e1) {
        System.err.println(e1.getLocalizedMessage());
    } catch (IOException e) {
        System.err.println(e.getLocalizedMessage());
    }
    file = null;
    render(view:'save',model:[images:img])
downloadedfile=request.getFile('sourceFile');
println“下载文件->”+下载文件
File destFile=新文件('web-app/source pdf/'+downloaddedfile+'.pdf');
如果(destFile.exists()){
destFile.delete();
}
File=null;
试一试{
file=新文件('web-app/source pdf/'+downloaddedfile+'.pdf');
downloadedfile.transferTo(文件)
println“文件->”+文件
}捕获(例外e){
System.err.println(“文件已在使用”)
//out.close();
}
File imageFile=新文件(“web app/pdf图像”);
if(imageFile.isDirectory())
{
String[]list=imageFile.list()

对于(int i=0;iIt可能只有我一个人,但我真的不知道你在这里问什么…在我的项目中,用户要求他可以上传pdf文件并将其转换为grailsSee中的图像文件,这是一个java解决方案!可能只有我一个人,但我真的不知道你在这里问什么…在我的项目用户ha中他可以上传pdf文件并将其转换为grailsSee中的图像文件,这是一个java解决方案!