skia2/site/user/sample/pdf.md

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 = ....;

    SkAutoTUnref<SkDocument> pdfDocument(
            SkDocument::CreatePDF(outputStream));

    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();
    }

    SkTArray<SkDocument::Attribute> info;
    info.emplace_back(SkString("Title"), SkString("...."));
    info.emplace_back(SkString("Author"), SkString("...."));
    info.emplace_back(SkString("Subject"), SkString("...."));
    info.emplace_back(SkString("Keywords"), SkString("...."));
    info.emplace_back(SkString("Creator"), SkString("...."));
    SkTime::DateTime now;
    SkTime::GetDateTime(&now);
    pdfDocument->setMetadata(info, &now, &now);

    return pdfDocument->close();
}