5

I have a PDF file, containing a 3D object in PRC format. I try to include this into a LaTeX document to be compiled with pdflatex, with the simple source below:

\documentclass{minimal}
\usepackage{graphicx}
\begin{document}
\includegraphics{LC.pdf}
\end{document}

Running pdflatex (version 3.1415926-2.3-1.40.12, TeX Live 2011) over this does not yield any error or warning, but the PDF file created does not include the 3D content (in fact, it is empty). The log file for my compilation appears happy, however:

<LC.pdf, id=1, 200.74097pt x 196.72597pt>
File: LC.pdf Graphic file (type pdf)
 <use LC.pdf>
Package pdftex.def Info: LC.pdf used on input line 4.
(pdftex.def)             Requested size: 200.74046pt x 196.72548pt.
 [1 <./LC.pdf>] (./a.aux) )

So: what am I doing wrong? How can I include this PDF figure, keeping its 3D content, into a LaTeX document?

Martin Scharrer
  • 262,582
F'x
  • 3,011
  • 2
    I guess the 3D object is implemented using PDF annotations. These are dropped by pdftex because AFAIK they only work reliable in the main PDF, not in an included PDF. The same is true for hyperlinks in included PDFs. I think there might be a way to have them, but pdftex doesn't know how to handle included PDF annotations in general. See How to preserve hyperlinks in included pdf? which might work as duplicate. – Martin Scharrer Apr 15 '12 at 14:21
  • @Martin Well, I'm sure it can be done, because Acrobat and at least one other PDF editor manage to do it (and I have not yet experienced an issue with this)! But it might be that pdftex implementation is lacking in this respect. It is be a bit sad if, after generating my PDF with pdflatex, I have to include my 3D graphs using Acrobat Pro… – F'x Apr 15 '12 at 14:35
  • It looks to me like that. You could ask the question on comp.text.tex as well, there it will be noticed by the pdftex gurus/developers, which might give you an authorized answer on this topic. – Martin Scharrer Apr 15 '12 at 14:38
  • @Martin thanks, I have now asked on c.t.t – F'x Apr 15 '12 at 14:46
  • What PDF viewer are you using? I don't think that every PDF viewer is capable of displaying 3D objects like Adobe Reader or Acrobat. – egreg Apr 15 '12 at 14:59
  • 1
    @egreg I know; I am using Acrobat X Pro (and I also checked with Adobe Reader 9) – F'x Apr 15 '12 at 15:00
  • @MartinScharrer: That seems to be what's happening; at least the 3d stuff is made out of annotations, which don't survive the inclusion. – Martin Schröder Apr 16 '12 at 06:30
  • @MartinScharrer, someone in that conversation suggested that lualatex should be able to keep the annotations in the included PDF http://tex.stackexchange.com/a/33915/1871. (I never succeeded myself to make it work.) – alfC May 23 '13 at 02:30

1 Answers1

15

As Martin said, interactive parts of a PDF, called Annotations in PDF specification parlance, such as links or 3D objects, get lost when embedding a PDF containing them. Instead, Annotations have to be re-generated for the new PDF.

To embed a 3D object in the PRC format into a PDF, use LaTeX package media9. See media9 manual, section "3D quick-start guide".

In case the standalone PRC file is not available it can be extracted from the PDF. This can be done manually, as explained below, or automatically with the help of a small Perl script. Either method requires a tool for uncompressing PDFs, such as PDFtk.

Extracting the PRC from PDF is not recommended if the PRC was generated by asymptote and you have the asy source file of it! Standalone PRC files are generated from asy source by

    asy --keep --tex pdflatex mysource.asy

Automatically, using PDFtk + Perl script

This extracts all PRC streams from PDFwithPRC.pdf to separate files prc-0.prc, prc-1.prc, ...

pdftk PDFwithPRC.pdf output - uncompress | perl prcextract.pl

On Windows, with a Java runtime and some perl.exe installed:

java -jar pdftk-all.jar PDFwithPRC.pdf output - uncompress | perl prcextract.pl

Perl script prcextract.pl:

#!/usr/bin/perl

$prc=0; $stream=0; $cnt=0;

while(<>){ if(/^stream/) {$stream=1;} elsif(/^endstream/) {$stream=0; if($prc){close $PRC;} $prc=0;} elsif((/^PRC/ || $prc) && $stream) { if(!$prc){open $PRC,">prc-".$cnt++.".prc";select $PRC} print; $prc=1; } }

Manual procedure, by PDFtk + text editor

First uncompress the PDF:

pdftk doc.pdf output doc.unc.pdf uncompress

Open doc.unc.pdf in a text editor and scroll down to a line that starts with PRC. This line appears just after a line with the PDF keyword stream.

Delete everything from begin of the file up to and including the line containing the stream keyword.

Delete everything beginning with the line starting with the endstream keyword directly after the PRC stream upto the end of the file.

Save what has left as a file whose name ends in .prc.

AlexG
  • 54,894
  • media9 allows include of PRC files into PDF, but not PRC-containing PDF files into PDF. Do you have a solution to extract a PRC from a PDF? – F'x Apr 16 '12 at 08:34
  • It's possible. See my edit. – AlexG Apr 16 '12 at 08:58
  • Your example file was generated using asymptote. In that case it is much better to create a standalone PRC from the asy source file and include it with media9. See my answer to your previous question. – AlexG Apr 16 '12 at 09:03
  • extracting the PRC stream like that and including it later with media9 looses all the valuable information: size & aspect ratio, viewpoint, external javascript, etc. I'm afraid it just doesn't work: there is more to the 3D PDF graphics than just the PRC stream… – F'x Apr 16 '12 at 09:53
  • 1
    @Regarding Asymptote, yes, I understand it's easier with the PRC file… but then again, you need not only the .prc but a lot of auxiliary files, which is a pain (compared to the simple PDF). In the end, the answer seems to be that pdftex simply doesn't support this kind of inclusion, and no one is interested enough to fix it (I am interested, but not to the point of hacking pdftex without any prior experience!). – F'x Apr 16 '12 at 09:55
  • 1
    Under the hood, Asympote uses my old movie15.sty package for including the PRC into PDF. It is obsolete and to be replaced by the new media9. There is not much about the additional JavaScript file you are referring to and which Asymtote attaches. It only enables billboard behaviour of textlabels (labels always facing the camera) and setting orthographic view instead of perspective view. All this can be done easily with media9 (add3Djscript=asylabels.js, 3Dortho). The optimal viewpoint, size&aspect ration can be set from within Reader, just right-click on Generate Default View. – AlexG Apr 16 '12 at 10:20
  • @F'x: If you want to add support for this to some engine, please add it to LuaTeX - it's probably already possible in Lua (and if not, the missing pieces can be added). – Martin Schröder Apr 16 '12 at 14:29
  • Do you know if it is possible to do the same with a U3D file embedded in a PDF? – Doellner Sep 10 '15 at 08:48
  • I tried this approach but cannot get it to work. I used the really basic example:
    \documentclass{report}
    \usepackage{media9}
    \begin{document}
    \includemedia[width=10 cm,activate=pageopen, 3Dmenu]{}{boxunc.prc}
    \end{document}
    
    

    The latest release of Acrobat Reader gives the error that the 3Dformat is supported by a more recent release. (Which is not possible). The original prc comes from a pdf that can be opened and works. (How do I upload a prc file here?)

    – Michel Janssens Oct 02 '20 at 08:25
  • @MichelJanssens You need to specify height as well, since the last-but-one argument (poster text) is empty. Given the last argument is a valid PRC file, continue to follow the 3D tutorial in the media9 documentation. – AlexG Oct 02 '20 at 08:52
  • Thanks Alex, The result is the same when I specify the height. I think the problem is the prc-file. The solution describes how to extract a prc-file from an existing pdf. I have an existing pdf with a working 3D in it. I follow the procedure described above and that does not work. (I must admit I have no idea what information is stored where in the prc, it is binary.) – Michel Janssens Oct 02 '20 at 09:52
  • If I leave the lines stream and endstream around the binary stuff, it does not give the error but acrobat reader crashes (after a while) when I do "generate default view" as is described in the media9 documentation. – Michel Janssens Oct 02 '20 at 14:29
  • Delete stream and endstream. You only need the binary stuff in between. – AlexG Oct 02 '20 at 19:07
  • Then I get the error I mentioned. – Michel Janssens Oct 03 '20 at 15:15
  • @MichelJanssens Could you please make the PDF containing the PRC file available for download (dropbox, perhaps)? – AlexG Oct 05 '20 at 11:31
  • Dear AlexG,Sorry it took so long. Here is a small file with just a box. Thanks in advance for looking at it. https://www.dropbox.com/t/vriFB1RT8c7hm2HH – Michel Janssens Oct 15 '20 at 09:46
  • @MichelJanssens : This is the PRC I extracted from your example: box.prc , and this the embedding example I get by strictly following the 3D quick-start guide (section 7.2 of the media9 manual): boxWithMedia9.tex, boxWithMedia9.pdf – AlexG Oct 15 '20 at 10:40
  • Thanks, your PRC file works here too. I guess I am doing something wrong with the extraction of the prc-file from the pdf. You already have the normal pdf. Here is the uncompressed pdf I use: https://www.dropbox.com/t/DVFDdRhCeCkaNIr2 – Michel Janssens Oct 15 '20 at 11:45
  • and this is the prc file I get out of that: https://www.dropbox.com/t/WrnRHVigRCvXkWf6 I am obviously doing something wrong. – Michel Janssens Oct 15 '20 at 11:47
  • @MichelJanssens I get the same error if I use the standard Windows Notepad text editor for extracting the PRC. Usually, I work with Vim on Linux. Could you try some advanced editor, say, Notepad++ ? – AlexG Oct 15 '20 at 12:06
  • It works now (Notepad++ for extraction). Thanks a lot. You really helped me out. – Michel Janssens Oct 15 '20 at 12:22
  • I even don't get it correctly with Notepad++. Did modify the encoding when saving as PRC? – AlexG Oct 15 '20 at 12:28
  • Yes, I think it has to do with the encoding. Default Notepad++ is in ANSI (for ASCII). Therefore, it does not work if you copy the binary stream. It works only if you delete the ASCII stuff and then save as... – Michel Janssens Oct 15 '20 at 12:53
  • @MichelJanssens I added a script based method, in case you are interested. – AlexG Oct 15 '20 at 14:20
  • Thanks, I already scripted it so it is one operation now. (This also eliminates the encoding problem.) Regards, Michel – Michel Janssens Oct 16 '20 at 08:54