Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-83164

Writing PDF data to the clipboard doesn't work

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.9.8
    • GUI: Printing
    • None
    • macOS

    Description

      I want to copy PDF data to the clipboard if a form that is recognized by other apps. I've written Cocoa apps before that do this pretty easily:

      NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
      [pasteboard declareTypes:@[NSPDFPboardType] owner:self];
      [self writePDFInsideRect:[self bounds] toPasteboard:pasteboard];

      Doing this with Qt is also pretty smooth; a few more lines of code, but the logic is clear enough:

      QBuffer buffer;
      buffer.open(QIODevice::WriteOnly);

      { QSize graphSize = size(); QPdfWriter pdfwriter(&buffer); QPageSize pageSize = QPageSize(graphSize, QString(), QPageSize::ExactMatch); QMarginsF margins(0, 0, 0, 0); pdfwriter.setResolution(72); // match the screen? pdfwriter.setPageSize(pageSize); pdfwriter.setPageMargins(margins); QPainter painter(&pdfwriter); drawContents(painter); // draws with the supplied painter }

      buffer.close();
      QMimeData mimeData;
      mimeData.setData("application/pdf", buffer.data());
      QGuiApplication::clipboard()->setMimeData(&mimeData);

      And mostly this seems to work. The buffer contains stuff that looks like legit PDF data, and indeed, if I write those bytes to a file the file will open in Preview.app as a PDF and display correctly. So, great! The problem is that if I go to Preview.app and select "New from Clipboard", it is unable to recognize the clipboard data as being PDF data; it just beeps. With the Cocoa code above, I get PDF data on the clipboard that Preview is correctly able to use with "New from Clipboard". (Other macOS apps similarly will work with the PDF clipboard data from my Cocoa app, but not with the data from my Qt app.)

      The problem is, I think, to do with the MIME type. If I use Apple's "Clipboard Viewer" utility, and look at the clipboard data produced by my Qt app, it is labeled as "com.trolltech.anymime.application--pdf". If I look at PDF data from my Cocoa app (or any other PDF-producing Cocoa app), Clipboard Viewer shows the clipboard as containing two data types: "com.adobe.pdf" and "Apple PDF pasteboard type". Apparently the MIME type used by Qt is not accepted on macOS.

      Of course "application/pdf" is the standard MIME type for PDF data, and ought to work; but I also tried putting in "com.adobe.pdf" and "Apple PDF pasteboard type" as MIME types to see if that would help. In Clipboard Viewer, however, they get displayed as "com.trolltech.anymime.com.adobe.pdf" and "com.trolltech.anymime.Apple PDF pasteboard type" respectively, and neither of those is compatible with Preview and other macOS apps, either. So I appear to be stuck.

      Ideally, using "application/pdf" should produce the correct results, I would think; but if there's a workaround that gets this to work in some other way, I would welcome that. It also seems like perhaps PDF is a standard enough interchange format that there should just be a presupplied method for it, like setText(), setHtml(), etc.

      Incidentally, I wonder if a hint might be that "application/pdf" comes out as "com.trolltech.anymime.application-pdf"; it looks like the forward slash was replaced by two dashes, which maybe breaks the MIME standard. Maybe this is some kind of file path correction, since '/' is a magic character in file paths, that is being erroneously applied to MIME types in this API? Anyway, that's just a wild guess. :> Thanks!

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            bhaller Ben Haller
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes