Android图形库Skia(四)-生成PDF

Android图形库Skia()-生成PDF


        本文主要记录使用skia库生成pdf文件的过程,其实skia并不仅仅能在Android系统中使用,在一般的嵌入式Linux系统上也可以使用的。标题就以以前的写法写了。本文基于Android图形库Skia()-基本测试生成PNG图片Android图形库Skia()-结合Freetype显示文字。直接给出例子:

/* Simple PDF demo utilizing Skia toolkit.
 * Authored by kangear <kangear@163.com>
 */
#include "SkBitmap.h"
#include "SkDevice.h"
#include "SkPaint.h"
#include "SkRect.h"
#include "SkImageEncoder.h"
#include "SkPDFCatalog.h"
#include "SkPDFDevice.h"
#include "SkPDFStream.h"
#include "SkPDFTypes.h"
#include "SkPDFDocument.h"
#include <stdio.h>
#include <unistd.h>
#include "SkTypeface.h"

//draw_content()

int main()
{
    // Declare a raster bitmap, which has an integer width and height,
    // and a format (config), and a pointer to the actual pixels.
    // Bitmaps can be drawn into a SkCanvas, but they are also used to
    // specify the target of a SkCanvas' drawing operations.
    SkBitmap bitmap;
    bitmap.setConfig(SkBitmap::kARGB_8888_Config, 200, 200);
    bitmap.allocPixels();

    // PDF
    SkPDFDevice* dev = new SkPDFDevice(827, 1169);
    SkAutoUnref aur(dev);

    // A Canvas encapsulates all of the state about drawing into a
    // device (bitmap).  This includes a reference to the device itself,
    // and a stack of matrix/clip values. For any given draw call (e.g.
    // drawRect), the geometry of the object being drawn is transformed
    // by the concatenation of all the matrices in the stack. The
    // transformed geometry is clipped by the intersection of all of the
    // clips in the stack.
    SkCanvas canvas(dev);

    // SkPaint class holds the style and color information about how to
    // draw geometries, text and bitmaps.
    SkPaint paint;

    // SkIRect holds four 32 bit integer coordinates for a rectangle.
    SkRect r;

    paint.setARGB(255, 255, 0, 0);
    r.set(25, 25, 145, 145);
    canvas.drawRect(r, paint); /** Draw the specified rectangle using
         the specified paint. The rectangle
         will be filled or stroked based on
         the Style in the paint. */

    paint.setARGB(255, 0, 255, 0);
    r.offset(20, 20);
    canvas.drawRect(r, paint);

    paint.setARGB(255, 0, 0, 255);
    r.offset(20, 20);
    canvas.drawRect(r, paint);
    const char gText[] = "123平34凡之路12!";
    //SkTypeface *font = SkTypeface::CreateFromFile("DroidSansFallbackFull.ttf");
    //usr/share/fonts/truetype/msttcorefonts/DroidSansFallbackFull.ttf
    SkTypeface *font = SkTypeface::CreateFromName("Droid Sans Fallback", SkTypeface::kNormal);
    if ( font )
    {
      paint.setARGB(255, 255, 0, 0);
      paint.setTypeface( font );
      paint.setTextSize(25);
      canvas.drawText(gText, sizeof(gText)/sizeof(gText[0]), 0, 25, paint);
    }
    else
    {
      printf("font ==null !\n");
    }

    // SkImageEncoder is the base class for encoding compressed images
    // from a specific SkBitmap.
    SkImageEncoder::EncodeFile("snapshot.png", bitmap,
    SkImageEncoder::kPNG_Type,
    /* Quality ranges from 0..100 */ 100);

 

    // save as a pdf file

    SkPDFDocument doc;
    doc.appendPage(dev);
    SkDynamicMemoryWStream pdf_stream;
    doc.emitPDF(&pdf_stream);
    write(STDOUT_FILENO, pdf_stream.getStream(), pdf_stream.getOffset());
 

    return 0;
}

 

/*

build:

g++ \
-I./include \
-I./include/core \
-I./include/images  \
-I./include/config \
-I./include/pdf/ \
-Wall -o test-skia ./test-skia.cpp \
out/src/images/SkImageDecoder_libpng.o \
out/libskia.a \
`pkg-config freetype2 --libs --cflags` \
`pkg-config libpng --libs --cflags` \
-lpthread -g
 

run:
./test-skia  > test.pdf

*/


效果:

 

        注意事项:生成PDF时,如果使用SkTypeface::CreateFromFile方法创建字体face,属于内置字体,会造成在其它环境下PDF中字体无法正常显示。使用Adobe reader会提示:

 

         打印效果:


文字版本:

Cannot extract the embedded font ‘DroidSansFallback’. Some characters may not display or print correctly.

 

参考文档:

1.https://code.google.com/p/skia/wiki/PDFTheoryOfOperation

2.List_of_typefaces

3.PDFReference



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁保康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值