Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF with an external image using XObject

Tags:

pdf

I'm trying to build a PDF file with a link to an external file.

I'm using the spec https://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf

On page 348 there is an example of image with an alternate image loaded remotely. When I create a document with the example from the doc, the reader (using acrobat reader XI) doesn't fetch the image. There is no error message but no request is being made (checked using wireshark).

  1. Can I have only a remote image (ie no "normal" image and alternate image).
  2. Is there an example somewhere of a full document using that /FS /URL syntax (ie not just the objects)? I couldn't find any that actually does the request.

Thanks

Edit: I used LibreOffice to create the base document with a single 1x1 pixel. http://pastebin.com/5GqCYgMp

I initially created my test document with Acrobat but the output was really messy.

Then replaced the stream with the example from the pdf spec, and tried to fix the startxref offset, but not sure it's correct. http://pastebin.com/BT42g02P

This document is currently not opening correctly, but I tried to get a minimum test case. My previous attempts were displayed with no errors only by luck (but the remote image didn't work anyway).

Is there any tool that actually allows the creation of XObject with /URL? I don't know the file format enough to create them reliably by hand.

like image 680
Nicolas Esteves Avatar asked Mar 30 '16 19:03

Nicolas Esteves


People also ask

Can you embed an image in a PDF?

Place an image or object into a PDFOpen the PDF in Acrobat, and then choose Tools > Edit PDF > Add Image . In the Open dialog box, locate the image file you want to place. Select the image file, and click Open. Click where you want to place the image, or click-drag to size the image as you place it.

Can you add images to an interactive PDF?

PDF forms are one of the most convenient formats for data collection, sharing and storage. Apart from text, you can insert images into PDF files by taking a photo with your mobile device or uploading it from the gallery.


1 Answers

First of all,

I'm using the spec https://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_reference_1-7.pdf

I would recommend not using a PDF reference but instead the ISO standard. The Adobe PDF references are not normative in nature while the ISO standard is. (The actual content differences are minute but if there is a normative spec, one should use it.) Adobe also publishes a copy of the ISO standard with merely the header exchanged.

Then, please don't treat PDFs as text documents. E.g. by sharing them on pastebin, you make them subject to treatment as text which essentially destroys the content.

That all been said, let's look at your actual issue:

In your sample PDF you have:

4 0 obj
<</Type/XObject/Subtype/Image/Width 1/Height 1/BitsPerComponent 8/Length 0/F << /FS /URL
/F ( https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png )
>>/Filter/FlateDecode/ColorSpace/DeviceRGB
>>
stream
endstream
endobj

This indicates that the PDF viewer shall find at the URL https://upload.wikimedia.org/wikipedia/commons/c/ca/1x1.png a file containing an array of 1 (/Width 1/Height 1) RGB (/ColorSpace/DeviceRGB) sample with 1 byte per color (/BitsPerComponent 8), cf. section 8.9.5 Image Dictionaries of ISO 32000-1.

I doubt your file fulfills that, I assume it actually is a PNG file in particular with a PNG structure, not the structure explained above.

PDF does not support the PNG format as is, you have to transform the data. It does support, though, the JPEG format using the /FFilter /DCTDecode which is why the sample from the specification

16 0 obj
  << /Type /XObject
     /Subtype /Image
     /Width 1000
     /Height 2000
     /ColorSpace /DeviceRGB
     /BitsPerComponent 8
     /Length 0 % This is an external stream
     /F << /FS /URL
           /F (http://www.myserver.mycorp.com/images/exttest.jpg)
        >>
     /FFilter /DCTDecode
  >>
stream
endstream
endobj

makes it look so easy.

like image 103
mkl Avatar answered Jan 02 '23 23:01

mkl