From 44ce296581f2f604bae8baba83dc4cc43677561c Mon Sep 17 00:00:00 2001 From: Johannes 'josch' Schauer Date: Fri, 20 Jan 2017 09:14:36 +0100 Subject: [PATCH] README.md: add more examples of how to use the library --- README.md | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 27637d6..9e67735 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,33 @@ You can then test the converter using: The package can also be used as a library: import img2pdf - pdf_bytes = img2pdf.convert('test.jpg') - file = open("name.pdf","wb") - file.write(pdf_bytes) + # opening from filename + with open("name.pdf","wb") as f: + f.write(img2pdf.convert('test.jpg')) + + # opening from file handle + with open("name.pdf","wb") as f1, open("test.jpg") as f2: + f1.write(img2pdf.convert(f2)) + + # using in-memory image data + with open("name.pdf","wb") as f: + f.write(img2pdf.convert("\x89PNG...") + + # multiple inputs (variant 1) + with open("name.pdf","wb") as f: + f.write(img2pdf.convert("test1.jpg", "test2.png")) + + # multiple inputs (variant 2) + with open("name.pdf","wb") as f: + f.write(img2pdf.convert(["test1.jpg", "test2.png"])) + + # writing to file descriptor + with open("name.pdf","wb") as f1, open("test.jpg") as f2: + img2pdf.convert(f2, outputstream=f1) + + # specify paper size (A4) + a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297)) + layout_fun = img2pdf.get_layout_fun(a4inpt) + with open("name.pdf","wb") as f: + f.write(img2pdf.convert('test.jpg', layout_fun=layout_fun))