skia2/site/user/sample/pdf.md
halcanary 2c9c469cc9 SkPDF: metadata first
Motivation: while experimenting with serializing images immediately, I would like to keep serializing metadata first.

TBR=djsollen@google.com
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1780463008

Review URL: https://codereview.chromium.org/1780463008
2016-03-20 07:07:11 -07:00

1.3 KiB

Using Skia's PDF Backend

Here is an example of using Skia's PDF backend in the recommended way: via the SkDocument and SkCanvas APIs.

#include "SkDocument.h"

bool WritePDF(SkWStream* outputStream) {
    sk_sp<SkDocument> pdfDocument(SkDocument::CreatePDF(outputStream));
    typedef SkDocument::Attribute Attr;
    Attr info[] = {
        Attr(SkString("Title"),    SkString("....")),
        Attr(SkString("Author"),   SkString("....")),
        Attr(SkString("Subject"),  SkString("....")),
        Attr(SkString("Keywords"), SkString("....")),
        Attr(SkString("Creator"),  SkString("....")),
    };
    int infoCount = sizeof(info) / sizeof(info[0]);
    SkTime::DateTime now;
    SkTime::GetDateTime(&now);
    pdfDocument->setMetadata(info, infoCount, &now, &now);

    int numberOfPages = ....;
    for (int page = 0; page < numberOfPages; ++page) {
        SkScalar pageWidth = ....;
        SkScalar pageHeight = ....;
        SkCanvas* pageCanvas =
                pdfDocument->beginPage(pageWidth, pageHeight);

        // ....insert canvas draw commands here....

        pdfDocument->endPage();
    }
    return pdfDocument->close();
}