2

I'm trying to create a multi-page "PDF" (application/pdf Adobe Acrobat format) file with code similar to this minimum working example code, featuring PlotLegends and "LargeLabels"

Export["Test.PDF",
    CreateDocument[
        ExpressionCell[
            #
            , PageBreakBelow -> True
            , WindowSize->All
        ]& /@ Table[
            ListPlot[
                Sort/@RandomReal[1,{5,20}]
                , Joined -> True
                , PlotLegends -> RandomWord[5]
                , PlotTheme -> {"Scientific","LargeLabels"}
                , PlotLabel -> RandomWord[]
                , ImageSize-> 600
            ]
            ,{5}
        ]
    ]
    , "PDF"
]

The image shows the problem: Legends are clipped and the page is portrait instead of Landscape.

enter image description here

I'm struggling to find a good way to programmatically avoid PlotLegends being clipped, other than changing ImageSize by trial and error. As pages are created programmatically, checking all by hand is undesirable.

Also, I can't find out how to define the correct page orientation, i.e to force a Landscape page if AspectRatio $< 1$ and Portrait otherwise.

How can one create a multi-page PDF file with the correct page orientation and avoid clipping PlotLegends?

rhermans
  • 36,518
  • 4
  • 57
  • 149
  • 1
    I guess, the problem is in the determination of correct width of the plots. The ImageSize->600 is 600 printer points but it does not correlate with screen resolution and visible size of the plots.. – Rom38 Aug 17 '22 at 05:32

1 Answers1

2

Over the years of the site's existence, questions have regularly arisen about the export of multi-page PDF files, similar to this one, and for all this time an adequate solution without an involvement of third-party utilities has not been found. To my knowledge, using third party utilities is the only workable approach so far. It consists of two stages:

  1. Export each page as a separate PDF file.
  2. Merge all exported pages into one document using one of the third party utilities. I recommend PyMuPdf. Alternatively, one can use pdfunite from Poppler utils as shown here.
Alexey Popkov
  • 61,809
  • 7
  • 149
  • 368
  • Thanks, @AlexeyPopkov, you seem to be correct about this. Not the answer I was expecting, but I think likely the only good answer one can expect. – rhermans Aug 19 '22 at 08:15