PDFObject实现pdf文件预览

官方例子:

https://pdfobject.com/#examples

下面主要解决非固定路径的pdf文件浏览

pdf的路径作为参数传入 




    Show PDF
    
    
    


    

后端pdf参数绝对路径截取 子目录路径

	/**
	 * 在线打开pdf
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("/showPdf")
	public String showPdf(HttpServletRequest request, HttpServletResponse response, Model model) {

		String filepath= UriUtils.decode(request.getParameter("filepath"), "UTF-8");
		int index = filepath.indexOf("dir");
		filepath = filepath.substring(index+5, filepath.length());
		filepath = filepath.replaceAll("\\\\", "/");
		model.addAttribute("pdfpath",filepath);
		return "/modules/fileBroser/viewPagePdf.html";
	}

将pdf的绝对路径转换成项目的虚拟路径

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyPathConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
      registry.addResourceHandler("/files/**").addResourceLocations("file:E:/dir/subdir/");
    }
}

 

你可能感兴趣的:(java)