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

Time:2019-11-14

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 link description http://fliphtml5.com
Create an ebook example.

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

E-book reading plug-in based on HTML5 conversion pdf
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

The plug-in displays the JPG pictures actually used, which can be generated by PDF documents, including the generation of normal pictures and thumbnails.
As follows

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
     *@ param filename PDF filename
     *@ 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 thumbnail
                Imageio. Write (srcimage, type, new file ("D: \ \ data \ \ HTML5 ebook paging mobile \ \ files \ \ thumb \" + (I + 1) + ". + type)); // generate thumbnail
                
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
 
/**
     *Free to determine start and end pages
     *@ param fileaddress
     *@ param filename PDF filename
     *@ param indexofstart start page number of page conversion, starting 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();
        }
    }
    /**
     * thumbnails
     */
    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;    
        }
}