共计 4252 个字符,预计需要花费 11 分钟才能阅读完成。
基于 HTML5 转换 PDF 的电子书阅读插件
html5 部分
项目一般都是以 pdf.js 插件阅读 PDF 文件,但存在阅读体验不理想的情况,所以参考手机上的电子书阅读器,使用国外的链接描述 http://fliphtml5.com
制作电子书示例。
完整代码已上传 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 : 'http://www.html5.com/',
isFlipPdf : "False",
TableOfContentButtonVisible:"False",
searchTextJS:'javascript/search_config.js',
searchPositionJS:undefined
};
java 部分
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;
}
}
正文完