龙空技术网

基于HTML5转换PDF的电子书阅读插件

晕晕大作战 181

前言:

现在姐妹们对“apache2pdf”大体比较关切,你们都想要剖析一些“apache2pdf”的相关知识。那么小编在网络上汇集了一些关于“apache2pdf””的相关内容,希望我们能喜欢,看官们快快来了解一下吧!

概述

存在阅读要求的项目,一般都是以pdf.js插件阅读PDF文件,但存在阅读体验不理想的情况,所以参考手机上的电子书阅读器,使用国外的[链接描述][1] 制作电子书示例。

最终效果

效果如下

代码说明

完整代码已上传git,标题搜索即可。

插件功能强大,缩略图,鼠标翻页,放大缩小都可以,部分配置代码config.js如下

var bookConfig = {	appName:'flippdf',	totalPageCount : 0,	largePageWidth : 1080,	largePageHeight : 1440,	normalPath : "files/page/",	largePath : "files/large/",	thumbPath : "files/thumb/",		ToolBarsSettings:"",	TitleBar:"",	appLogoLinkURL:"",	bookTitle:"FLIPBUILDER",	bookDescription:"",	ButtonsBar:"",	ShareButton:"",		ThumbnailsButton:"",	ThumbnailsButtonVisible:"Hide",	ZoomButton:"",	ZoomButtonVisible:"No",	FlashDisplaySettings:"",	MainBgConfig:"",	bgBeginColor:"#cccccc",	bgEndColor:"#eeeeee",	bgMRotation:45,	pageBackgroundColor:"#FFFFFF",	flipshortcutbutton:"Hide",	BookMargins:"",	topMargin:10,	bottomMargin:10,	leftMargin:10,	rightMargin:10,	HTMLControlSettings:"",	linkconfig:"",	LinkDownColor:"#808080",	LinkAlpha:0.5,	OpenWindow:"_Blank",	BookMarkButtonVisible:'False',	productName : 'Demo created by Flip PDF',	homePage : '',	isFlipPdf : "False",	TableOfContentButtonVisible:"False",	searchTextJS:'javascript/search_config.js',	searchPositionJS:undefined};
## java部分 ##

插件显示实际是使用的jpg图片,可以由pdf文档生成,其中涉及的是正常图片和缩略图的生成。

具体如下

maven引入

<dependency>			<groupId>org.apache.pdfbox</groupId>			<artifactId>fontbox</artifactId>			<version>2.0.9</version>		</dependency>		<dependency>			<groupId>org.apache.pdfbox</groupId>			<artifactId>pdfbox</artifactId>			<version>2.0.9</version>		</dependency>

核心程序

import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.WritableRaster;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.rendering.PDFRenderer;public class pdftest {	public static void main(String[] args) {		String fileAddress="D:\\data";		String filename="qqq";		String type="jpg";		pdf2png(fileAddress,filename,type);	}	 /** * 转换全部的pdf * @param fileAddress 文件地址 * @param filename PDF文件名 * @param type 图片类型 */ public static void pdf2png(String fileAddress,String filename,String type) { // 将pdf装图片 并且自定义图片得格式大小 File file = new File(fileAddress+"\\"+filename+".pdf"); try { PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for (int i = 0; i < pageCount; i++) { BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI  //ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type)); ImageIO.write(image, type, new File("D:\\data\\html5-ebook-paging-mobile\\files\\mobile\\"+(i+1)+"."+type));  BufferedImage srcImage = resize(image, 240, 240);//产生缩略图 ImageIO.write(srcImage, type, new File("D:\\data\\html5-ebook-paging-mobile\\files\\thumb\\"+(i+1)+"."+type));//产生缩略图  } } catch (IOException e) { e.printStackTrace(); } }  /** *自由确定起始页和终止页 * @param fileAddress 文件地址 * @param filename pdf文件名 * @param indexOfStart 开始页 开始转换的页码,从0开始 * @param indexOfEnd 结束页 停止转换的页码,-1为全部 * @param type 图片类型 */ public static void pdf2png(String fileAddress,String filename,int indexOfStart,int indexOfEnd,String type) { // 将pdf装图片 并且自定义图片得格式大小 File file = new File(fileAddress+"\\"+filename+".pdf"); try { PDDocument doc = PDDocument.load(file); PDFRenderer renderer = new PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for (int i = indexOfStart; i < indexOfEnd; i++) { BufferedImage image = renderer.renderImageWithDPI(i, 144); // Windows native DPI // BufferedImage srcImage = resize(image, 240, 240);//产生缩略图 ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type)); } } catch (IOException e) { e.printStackTrace(); } } /** * 缩略图 */ private static BufferedImage resize(BufferedImage source, int targetW, int targetH) {	 	int type = source.getType();		 	BufferedImage target = null; 	double sx = (double) targetW / source.getWidth(); 	double sy = (double) targetH / source.getHeight(); 	if (sx > sy) {			 		sx = sy; 	 targetW = (int) (sx * source.getWidth()); 	} else  	{			 		sy = sx; 		targetH = (int) (sy * source.getHeight());		 	} 	if (type == BufferedImage.TYPE_CUSTOM) { 		ColorModel cm = source.getColorModel();	 		WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH); 		boolean alphaPremultiplied = cm.isAlphaPremultiplied();			 		target = new BufferedImage(cm, raster, alphaPremultiplied, null);	 		} else { 	 target = new BufferedImage(targetW, targetH, type);		 	}	 	Graphics2D g = target.createGraphics();		 	g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);	 	g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));		 	g.dispose();		 	return target;	 	}}

标签: #apache2pdf #html5pdf教程 #apache教程pdf #html5上传图片插件 #html5程序pdf