Skip to content

Exception thrown when coping pages from Weasyprint generated PDF #1017

@lofcz

Description

@lofcz

First, thanks for maintaining PdfPig. It's a great boon we've been using for ages. Recently, we started converting various text formats into PDFs via Pandoc with WeasyPrint as --print-engine. We consistently hit the following issue when copying pages from such a PDF (against 0754e7f):

UglyToad.PdfPig.Core.PdfDocumentFormatException: Expected a IndirectReferenceToken for the XObject, got a DictionaryToken
   at UglyToad.PdfPig.Writer.PdfPageBuilder.CopyFrom(Page srcPage)

Our code:

using PdfDocument document = PdfDocument.Open(fullPath);
await using FileStream outputStream = new FileStream(distPath, FileMode.Create, FileAccess.Write);
using PdfDocumentBuilder pdfWriter = new PdfDocumentBuilder();

for (int i = pageFrom; i <= pageTo; i++)
{
    Page page = document.GetPage(i);
    PdfPageBuilder newPage = pdfWriter.AddPage(page.Width, page.Height);
    newPage.CopyFrom(page);
}

byte[] newPdf = pdfWriter.Build();
await outputStream.WriteAsync(newPdf);

Sample PDF:
sample.pdf

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions