skia2/site/user/sample/pdf.md
Andrew Monshizadeh 9d6681cc70 Changes to site documentation
Mostly just formatting fixes with a few grammatical changes.

Two real notable changes:
  - Removed references to SkGLCanvas from Tips & FAQ and replaced with
    references to `SkDevice` and `SkSurface`.
  - Deleted deprecated "Quick Start Guides" folder

Docs-Preview: https://skia.org/?cl=92361
Bug: skia:
Change-Id: Ief790b1c2bae8fe0e39aa8d66c79f80560d18c9e
Reviewed-on: https://skia-review.googlesource.com/92361
Reviewed-by: Heather Miller <hcm@google.com>
Reviewed-by: Joe Gregorio <jcgregorio@google.com>
Commit-Queue: Joe Gregorio <jcgregorio@google.com>
2018-01-11 19:47:58 +00:00

3.2 KiB

Using Skia's PDF Backend

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

#include "SkDocument.h"

void WritePDF(SkWStream* outputStream,
              const char* documentTitle,
              void (*writePage)(SkCanvas*, int page),
              int numberOfPages,
              SkSize pageSize) {
    SkDocument::PDFMetadata metadata;
    metadata.fTitle = documentTitle;
    metadata.fCreator = "Example WritePDF() Function";
    SkTime::DateTime now;
    SkTime::GetDateTime(&now);
    metadata.fCreation.fEnabled  = true;
    metadata.fCreation.fDateTime = now;
    metadata.fModified.fEnabled  = true;
    metadata.fModified.fDateTime = now;
    sk_sp<SkDocument> pdfDocument = SkDocument::MakePDF(
            outputStream, SK_ScalarDefaultRasterDPI, metadata,
            nullptr, true);
    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:

  • drop means to draw nothing.

  • ignore means to draw without the effect

  • expand means to implement something in a non-PDF-ish way. This may mean to rasterize vector graphics, to expand paths with path effects into many individual paths, or to convert text to paths.

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:

  • SkImageFilter: When SkImageFilter is expanded, text-as-text is lost.

  • SkXferMode: The following transfer modes are not natively supported by PDF: DstOver, SrcIn, DstIn, SrcOut, DstOut, SrcATop, DstATop, and Modulate.

Other limitations:

  • drawText with VerticalText — drop. No known clients seem to make use of the VerticalText flag.

  • drawTextOnPath — expand. (Text-as-text is lost.)

  • drawVertices — drop.

  • drawPatch — drop.