c++svgmupdf

MuPDF SVG export bug?


I am trying to export PDF file as a SVG data, using the MuPDF library. I have written a following function for this task:

PDFIMPORT_DECLSPEC int PDFImport::createSVGDataStr( const std::string & filenamePDF, Buffer & outputBuffer, Feedback* fbck )
{
    std::array<std::mutex, FZ_LOCK_MAX> mutexes;

    fz_locks_context lockCtx;
    lockCtx.user   = mutexes.data();
    lockCtx.lock   = lock;
    lockCtx.unlock = unlock;

    fz_document* doc = nullptr;
    fz_context* ctx = fz_new_context(NULL, &lockCtx, FZ_STORE_UNLIMITED);
    fz_register_document_handler(ctx, &pdf_document_handler);
    fz_try(ctx)
    {
        doc = fz_open_document(ctx, const_cast<char*>(filenamePDF.c_str()));
    }
    fz_catch(ctx)
    { 
        fz_free_context(ctx);
        return Error_OpenFile;
    }

    int pageCount = fz_count_pages(doc);
    if (pageCount < 1)
        return Error_Content;

    fz_page* page = fz_load_page(doc, 0);

    fz_rect brect;
    fz_bound_page(doc, page, &brect);

    fz_buffer* buffer = fz_new_buffer(ctx, 1024);
    fz_output* output = fz_new_output_with_buffer(ctx, buffer);
    fz_device* device = fz_new_svg_device(ctx, output, brect.x1 - brect.x0, brect.y1 - brect.y0);

    fz_cookie* fc = reinterpret_cast<fz_cookie*>(fbck);
    fz_run_page(doc, page, device, &fz_identity, fc);


    fz_buffer_printf(ctx, buffer, "</svg>");

    outputBuffer.data   = buffer->data;
    outputBuffer.length = buffer->len;

    fz_free_device(device);
    fz_close_output(output);
    fz_free_page(doc, page);
    fz_free_context(ctx);

    return Error_None;
}



What I have found is, that there is always missing an enclosing </svg> tag at the end of data buffer. I have tested it with various PDF files and with MuPDF 1.3 and 1.5 with same result

So, my question is: Am I doing it wrong or is there a bug in the MuPDF SVG device? Have somebody any experinece with this?
Thanks!


Solution

  • This is not a bug! You have to close the device first. If you don't close it, it is not adding </svg> at the end of the document. Also you can instantiate the output without a buffer.

    fz_rect brect;
    fz_bound_page(ctx, page, &brect);
    
    fz_output* output = fz_new_output_with_path(ctx, "output.svg", 0);
    fz_device* device = fz_new_svg_device(ctx, output, brect.x1 - brect.x0, brect.y1 - brect.y0, 0, 0);
    
    fz_run_page_contents(ctx, page, device, &fz_identity, NULL);
    
    fz_close_device(ctx, device);
    fz_close_output(ctx, output);
    
    fz_drop_output(ctx, output);