E-book reading plug-in based on HTML5 conversion PDF

Keywords: Java html5 Mobile Apache

E-book reading plug-in based on HTML5 conversion PDF

Part html5

Generally, the project uses the pdf.js plug-in to read the PDF file, but the reading experience is not ideal, so refer to the e-book reader on the mobile phone, and use the foreign e-book reader Link descriptionhttp://fliphtml5.com
Create an ebook example.


The complete code has been uploaded to git, and the title search is enough.

The plug-in has powerful functions, such as thumbnail, mouse page turning, zooming in and out. Some configuration codes are as follows

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
};

Part java

Introduction of 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>

Core procedure

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);
    }
     /**
     * Convert all PDFs
     * @param fileAddress File address
     * @param filename PDF file name
     * @param type Picture type
     */
    public static void pdf2png(String fileAddress,String filename,String type) {
        // Loading pdf with pictures and customizing the format size of pictures
        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);//Generate thumbnails
                ImageIO.write(srcImage, type, new File("D:\\data\\html5-ebook-paging-mobile\\files\\thumb\\"+(i+1)+"."+type));//Generate thumbnails
                
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
 
/**
     *Free to determine start and end pages
     * @param fileAddress File address
     * @param filename pdf file name
     * @param indexOfStart Start page number to convert from 0
     * @param indexOfEnd End page number to stop conversion, - 1 is all
     * @param type Picture type
     */
    public static void pdf2png(String fileAddress,String filename,int indexOfStart,int indexOfEnd,String type) {
        // Loading pdf with pictures and customizing the format size of pictures
        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); / / generate thumbnail
                ImageIO.write(image, type, new File(fileAddress+"\\"+filename+"_"+(i+1)+"."+type));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * thumbnail
     */
    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;    
        }
}

Posted by Gustav on Wed, 06 Nov 2019 08:28:53 -0800