在网页中显示PDF

时间:2013-11-02 04:43:09

标签: java html spring pdf pdf-conversion

要跟进此post,我将代码更改为:

@RequestMapping( value = "/{prePath:^tutor$|^admin$}/module/{file_id}" )
public void getModule( @PathVariable( "file_id" )
int fileId, Model model, HttpServletResponse response, HttpServletRequest request )
{
     model.addAttribute( "id", fileId );

     File test = new File( "C:\\resource\\pdf\\test.pdf" );
     response.setHeader( "Content-Type", "application/pdf" );
     response.setHeader( "Content-Length", String.valueOf( test.length() ) );
     response.setHeader( "Content-Disposition", "inline; filename=\"test.pdf\"" );

     System.out.println( test.toPath() );
     try
     {
         Files.copy( test.toPath(), response.getOutputStream() );
     }
     catch( IOException e )
     {
         e.printStackTrace();
     }
}

最后能够在网页上显示PDF。 URL可通过以下方式访问:

<a href="../admin/module/${ file_id }.do?test" >Spring Tutorial</a>

但PDF文件显示在整个页面上。我的PDF来自我的本地,我想用一部分网页显示它。也许是<div>或任何最适合这种方法的东西。有关如何做到这一点的想法?

2 个答案:

答案 0 :(得分:0)

如果用户有adobe pdf,那么只需iframe它就可以在浏览器中查看     

答案 1 :(得分:0)

<?php
$file = "file.pdf";
$read = file_get_contents($file);
echo $read;
?>