Google Code Prettify

2012年9月20日 星期四

JMuPdf PDF to Image

MuPdf 實在有夠強大的,解析檔案出現字型問題,還是能夠正常輸出畫面

JMuPdf JNI library http://code.google.com/p/jmupdf/

轉jpg範例


PdfDocument pdfDoc = new PdfDocument(pdf_file);
int count = pdfDoc.getPageCount();
int zoom = 1;

for(int i=0;i < count ;i++){
     PageRenderer render = new PageRenderer(pdfDoc.getPage((i+1)), zoom, Page.PAGE_ROTATE_AUTO, ImageType.IMAGE_TYPE_RGB);
     render.setAntiAliasLevel(8);//需設為8,不然中文字會變醜
     render.render(true);

     JPGOptions options = new JPGOptions();
     options.setQuality(96);

     FileOutputStream out = new FileOutputStream(new File(out_dir+(i+1)+".jpg"));
     JimiWriter writer = Jimi.createJimiWriter("image/jpeg", out);
     writer.setSource(render.getImage().getSource());
     writer.setOptions(options);
     writer.putImage(out);
    
     out.flush();out.close();
    
     render.dispose();
}

pdfDoc.dispose();

解決canvas在行動裝置上模糊的問題

在高解析度的行動裝置上畫圖,一直會有模糊的問題

可以參考 http://www.html5rocks.com/en/tutorials/canvas/hidpi/

javascript 如下:


var devicePixelRatio = window.devicePixelRatio || 1,
var backingStoreRatio = context.webkitBackingStorePixelRatio ||
                            context.mozBackingStorePixelRatio ||
                            context.msBackingStorePixelRatio ||
                            context.oBackingStorePixelRatio ||
                            context.backingStorePixelRatio || 1,

var ratio = devicePixelRatio / backingStoreRatio;
if (devicePixelRatio !== backingStoreRatio) {

        var oldWidth = canvas.width;
        var oldHeight = canvas.height;

        canvas.width = oldWidth * ratio;
        canvas.height = oldHeight * ratio;

        canvas.style.width = oldWidth + 'px';
        canvas.style.height = oldHeight + 'px';
}

2012年7月2日 星期一

Apache http Server 2.4 效能解放


Apache 2.4 宣佈已經可以跟 Nginx比拼了
但很多網路文章卻測不出這個效能

一開始我也沒辦法解放它的效能
經過數次的安裝移除的循環
發現一個重要的參數: --with-mpm
必須安裝為 event Multi-Processing Module
這就是解放效能的所在囉

什麼是 event module, 其實就是 linux EPoll,Nginx 也是應用這個模式


Apache 2.4 安裝步驟

#安裝 APR
tar -zxvf apr-1.4.6.tar.gz
cd apr-1.4.6
./configure --prefix=/usr/local/apache-apr --enable-nonportable-atomics=yes
make
sudo make install

#安裝 APR-UTIL apr-util
tar -zxvf apr-util-1.4.1.tar.gz
cd apr-util-1.4.1
./configure --prefix=/usr/local/apache-apr --with-apr=/usr/local/apache-apr
make
sudo make install

unzip -o pcre-8.30.zip
cd pcre-8.30
./configure --prefix=/usr/local/pcre --enable-utf --enable-unicode-properties
make
sudo make install

#安裝 apache server
1. tar -zxvf httpd-2.4.2.tar.gz

2. cd httpd-2.4.2

3. ./configure --prefix=/usr/local/apache2.4 --with-apr=/usr/local/apache-apr --with-apr-util=/usr/local/apache-apr --with-pcre=/usr/local/pcre --with-mpm=event \
--enable-nonportable-atomics=yes \
--enable-mods-shared=all \
--enable-expires=shared \
--enable-headers=shared \
--enable-reqtimeout=shared \
--enable-ssl=shared \
--enable-rewrite=shared \
--enable-speling=shared \
--enable-proxy=shared \
--enable-proxy-connect=shared \
--enable-proxy-ajp=shared \
--enable-proxy-balancer=shared \
--enable-proxy-express=shared \
--enable-info=shared \
--enable-heartbeat=shared \
--enable-heartmonitor=shared \
--enable-ratelimit=shared \
--enable-remoteip=shared \
--enable-allowmethods=shared

4. make

5. sudo make install

6.Check Apache MPM 是 event
/usr/local/apache2.4/bin/httpd -l
/usr/local/apache2.4/bin/httpd -V | grep MPM

#效能調整
#修改 httpd-mpm.conf 下的mpm_event_module

    StartServers             2
    MinSpareThreads         25
    MaxSpareThreads         75
    ThreadsPerChild         25
    MaxRequestWorkers      150
    MaxConnectionsPerChild   0

利用 Ghostscript 重新產生PDF


Ghostscript
http://pages.cs.wisc.edu/~ghost/


完整參數
http://www.ghostscript.com/doc/current/Use.htm


範例指令

 轉換為600dpi
 gswin32c -dNOPAUSE -dBATCH -q -r600 -sDEVICE=pdfwrite -sOutputFile=out.pdf input.pdf

指定頁數
gswin32c -dNOPAUSE -dBATCH -q -r600 -sDEVICE=pdfwrite -dFirstPage=22 -dLastPage=36 -sOutputFile=out.pdf input.pdf

pdfwrite Additional Options

PDF optimization level selection options
 -dPDFSETTINGS=/screen   (screen-view-only quality, 72 dpi images)
 -dPDFSETTINGS=/ebook    (low quality, 150 dpi images)
 -dPDFSETTINGS=/printer  (high quality, 300 dpi images)
 -dPDFSETTINGS=/prepress (high quality, color preserving, 300 dpi imgs)
 -dPDFSETTINGS=/default  (almost identical to /screen)


Paper size selection options
 -sPAPERSIZE=letter
 -sPAPERSIZE=a4
 -dDEVICEWIDTHPOINTS=w -dDEVICEHEIGHTPOINTS=h (point=1/72 of an inch)
 -dFIXEDMEDIA (force paper size over the PostScript defined size)

Other options
 -dEmbedAllFonts=true
 -dSubsetFonts=false
 -dFirstPage=pagenumber
 -dLastPage=pagenumber
 -dAutoRotatePages=/PageByPage
 -dAutoRotatePages=/All
 -dAutoRotatePages=/None
 -r1200 (resolution for pattern fills and fonts converted to bitmaps)
 -sPDFPassword=password
 -dConvertCMYKImagesToRGB=true
 -dCompatibilityLevel=1.5

更多參數請參考 Ps2pdf.htm