0

I can't find a solution that works for me.

I have a number of already existing figures i created earlier and want to combine them into one pdf landscape page. the graphic files are standard matplotlib savefig's as .png.

So how can I simply combine, say four, plots to save them as one pdf page with python? I would like to arrange them like this:

*-----------------------------------------------------------------------------------*
|                                                                                   |
|    *-------------------------------*         *-------------------------------*    |
|    |                               |         |                               |    |
|    |                               |         |                               |    |
|    |         fig 1                 |         |       fig 2                   |    |
|    |                               |         |                               |    |
|    |                               |         |                               |    |
|    *-------------------------------*         *-------------------------------*    |
|                                                                                   |
|                                                                                   |
|    *-------------------------------*         *-------------------------------*    |
|    |                               |         |                               |    |
|    |                               |         |                               |    |
|    |         fig 3                 |         |       fig 4                   |    |
|    |                               |         |                               |    |
|    |                               |         |                               |    |
|    *-------------------------------*         *-------------------------------*    |
|                                                                                   |
*-----------------------------------------------------------------------------------*

is there some very simple way to to this?

Thanks for the help,

Tobias

1
  • You can also use reportlab Commented Dec 13, 2014 at 10:29

2 Answers 2

2

How about using Inkscape? You can simply import the pngs and save the result as pdf.

If you need to do this for many pngs, you could save to svg in tnkscape and use it as a template. With a simple python script you could replace the filenames of the png source in the svg and use inkscape again to create pdfs.

Sign up to request clarification or add additional context in comments.

Comments

1

Have you considered making an html template that assembles the images in proper positions and then using a python pagkage (eg.: pdfkit) to convert your html "page" to a pdf document?

Would it be a valid solution for you?

Hope it helps!

---- EDIT ----

I got a working example using my suggestion. Assuming that you have already installed pdfkit and wkhtmltopdf as instructed in the link I sent previously, suppose you have the following files (I will show each file content ahead) in a directory called results:

  • results
    • fig1.png
    • fig2.png
    • fig3.png
    • fig4.png
    • template.html
    • style.css
    • html2pdf.py

The png files are obviously your plots image files.

The template.html file contains the following code:

<!doctype html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<html>
    <body>
        <div id="ext_container">
            <div id="row1">
                <div id="fig1_container">
                    <img src="fig1.png"> </img>
                </div>
                <div id="fig2_container">
                    <img src="fig2.png"> </img>
                </div>
            </div>
            <div id="row2">
                <div id="fig3_container">
                    <img src="fig3.png"> </img>
                </div>
                <div id="fig4_container">
                    <img src="fig4.png"> </img>
                </div>
            </div>
        </div>
    </body>
</html>

The style.css contains the following code:

ext_container {
    position: absolute;
    top: 50px;
}

#fig1_container {
    position: relative;
    float: left;
}

#fig2_container {
    position: relative;
    float: left;
    left: 100px;
}

#fig3_container {
    position: relative;
    float: left;
}

#fig4_container {
    position: relative;
    float: left;
    left: 100px;
}

#row2 {
    position: relative;
    top: 50px;
}

img {
    max-width: 300px;
    max-height: 300px;
}

And the html2pdf.py script contains the following lines (as documented in pdfkit usage):

import pdfkit
pdfkit.from_file('template.html', 'output.pdf')

Then all you have to do is calling the html2pdf.py script from command line.

Hope you get this working!

4 Comments

unfortunately i'm not that good in html and i'm simply looking for a way to create a pdf and place some pictures in it... i thought there would have to be some kind of easy way for this...
I'm trying to make a working example using the solution I suggested. I'll post it here as soon as I get it to work.
That looks actually really great, thank you very much for that. just one question... i'm getting an error, that pdfkit seems not to bee installed and installing wkhtmltopdf from your link didnt cure that...
Okay, i did manage to get it to work and now i simply have to adjust to my favored settings. thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.