Community Issues Reference Playground

Using Skia’s PDF Backend

Here is an example of using Skia’s PDF backend (SkPDF) via the SkDocument and SkCanvas APIs.

#include "SkPDFDocument.h"

void WritePDF(SkWStream* outputStream,
              const char* documentTitle,
              void (*writePage)(SkCanvas*, int page),
              int numberOfPages,
              SkSize pageSize) {
    SkPDF::Metadata metadata;
    metadata.fTitle = documentTitle;
    metadata.fCreator = "Example WritePDF() Function";
    SkTime::DateTime now;
    SkTime::GetDateTime(&now);
    metadata.fCreation = now;
    metadata.fModified = now;
    sk_sp<SkDocument> pdfDocument = SkPDF::MakeDocument(outputStream, metadata);
    assert(pdfDocument);

    for (int page = 0; page < numberOfPages; ++page) {
        SkCanvas* pageCanvas = pdfDocument->beginPage(pageSize.width(),
                                                      pageSize.height());
        writePage(pageCanvas, page);
        pdfDocument->endPage();
    }
    pdfDocument->close();
}

SkPDF Limitations

There are several corners of Skia’s public API that SkPDF currently does not handle because either no known client uses the feature or there is no simple PDF-ish way to handle it.

In this document:

Effect text images everything else
SkMaskFilter drop ignore ignore
SkPathEffect ignore n/a expand
SkColorFilter ignore expand ignore
SkImageFilter expand expand expand
unsupported SkXferModes ignore ignore ignore
non-gradient SkShader expand n/a expand

Notes:

Other limitations: