display pdf report in tkinter frame - pdf

I want to display pdf file in tkinter frame in Kiosk mode so that I can add print and other buttons too. I am using below code
from tkPDFViewer import tkPDFViewer as pdf
from tkinter import Tk, Button
class ShowPdf(pdf.ShowPdf):
def goto(self, page):
try:
self.text.see(self.img_object_li[page - 1])
except IndexError:
if self.img_object_li:
self.text.see(self.img_object_li[-1])
root = Tk()
Above code executed successfully but giving warning as below
Deprecation: 'getPixmap' removed from class 'Page' after v1.19 - use 'get_pixmap'.
Deprecation: 'getImageData' removed from class 'Pixmap' after v1.19 - use 'tobytes
Also unable to display PDF file in customize size.
Expert pls. guide me so that I can display pdf file in frame.
Thanks in Advance.

Related

scale pdf pages in Python/ pypdf2

I have a question related to this post with the current pypdf2 and python versions. Using this code from the Pypdf2 documentation I receive an empty page 0x0mm. scaling content works as it should.
My understanding ist that the result should be scaled content as well as page size;
Other posts show that it has worked inn the past, obviously not with the current pypdf version.
Do you have an idea?
Thanks in advance for your help.
from PyPDF2 import PdfReader, PdfWriter, Transformation
# Read the input
reader = PdfReader("pdffile.pdf")
page = reader.pages[0]
# Scaling the content - works
op = Transformation().scale(sx=0.7, sy=0.7)
page.add_transformation(op)
# Scaling page - returns empty page
page.scale_by(0.7)
# Write the result to a file
writer = PdfWriter()
writer.add_page(page)
writer.write("out-pg-transform.pdf")

Creating a complex PDF file according to precise instructions and meticulous specifications

[Sorry for my bad English]
We are developing an application that manages information about public transportation. The application should generate posters for signage at bus stops.
The posters should conform to detailed and strict regulatory rules, in every detail. Typography, colors, tables, lines, symbols, embedded images and much more.
We need to produce the poster as a PDF file, which will be sent for printing.
Our question: How to produce this file in a reliable and efficient way?
Do we should to create an HTML+CSS file, then use a library that converts HTML to PDF?
Can we trust the library to convert the HTML completely accurately?
Or we should to use libraries that generate PDF directly like iText.
Do they support creating a complex PDF according to exact specifications?
And what is the most suitable environment to do it?
Our first priority is dotnet core, but if there is no choice, we will also consider using python or node.
And a final question, to which field of knowledge does this belong? What skills are needed to perform the task? We want to publish a tender for this task, and don't know what to ask for.
disclaimer: I am the author of borb, the library used in this answer
In general, there are two kinds of PDF libraries.
high level libraries: These libraries allow you to easily add content (images, text, tables, lists, etc) without having to specify too much. It's easier for you (the user) but you're giving up precise control.
low level libraries: These libraries bring you (the user) down to the nitty gritty level of the PDF. You can manipulate content and place it at exact positions. You can define a color space (ensuring the color can be calibrated), etc. This also means you give up comfort. You can not (easily) split text, automatically flow content blocks, etc
borb allows you to do both. You can place content at exact coordinates, you can specify your own fonts, you can set colors using RGB, HSV, etc
You can also use a PageLayout which will take over most of the content-placement.
This is an example using absolute positioning:
from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import Paragraph
from borb.pdf import PDF
from borb.pdf.canvas.geometry.rectangle import Rectangle
from decimal import Decimal
def main():
# create Document
doc: Document = Document()
# create Page
page: Page = Page()
# add Page to Document
doc.add_page(page)
# define layout rectangle
# fmt: off
r: Rectangle = Rectangle(
Decimal(59), # x: 0 + page_margin
Decimal(848 - 84 - 100), # y: page_height - page_margin - height_of_textbox
Decimal(595 - 59 * 2), # width: page_width - 2 * page_margin
Decimal(100), # height
)
# fmt: on
# the next line of code uses absolute positioning
Paragraph("Hello World!").paint(page, r)
# store
with open("output.pdf", "wb") as pdf_file_handle:
PDF.dumps(pdf_file_handle, doc)
if __name__ == "__main__":
main()
And this is that same example using a PageLayout
from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import PageLayout
from borb.pdf import SingleColumnLayout
from borb.pdf import Paragraph
from borb.pdf import PDF
def main():
# create Document
doc: Document = Document()
# create Page
page: Page = Page()
# add Page to Document
doc.add_page(page)
# set a PageLayout
layout: PageLayout = SingleColumnLayout(page)
# add a Paragraph
layout.add(Paragraph("Hello World!"))
# store
with open("output.pdf", "wb") as pdf_file_handle:
PDF.dumps(pdf_file_handle, doc)
if __name__ == "__main__":
main()

What wizardry is being used to display Matplotlib color maps in an ipython console?

I am working with Matplotlib color maps, and I also happen to be working with the Spyder IDE, which has an ipython console.
As you can see from the screen shot, the ipython console showed me a graphical representation of the color map object. This was unexpected and very helpful.
Normally I expect to see a string representation of an object, as you might see from the print() function call. Function calls to print() and repr() are shown, and they produce text, as is more typical.
I would like my own code to output this graphical representation when it is generating output. I have been poking through the matplotlib.colors.Colormap internals, and so far I haven't been able to figure out how. What is ipython doing? How can I do the same?
Thanks!
This rather seems like a ipython/jupyter feature. ipython detects the object and produces automatically a plot to preview the colormap.
Here using jupyter:
IPython looks if an object has a _repr_html_; if so, it calls it and displays the output as HTML. Here's an example (I ran this in Jupyter but it works the same as long as you're running IPython):
class MyCoolObject:
def _repr_html_(self):
return ("<h1>hello!</h1> <p>this is some html </p>"
"I can even put images:"
"<img src='https://upload.wikimedia.org/wikipedia"
"/commons/thumb/3/38/Jupyter_logo.svg"
"/44px-Jupyter_logo.svg.png'></img>")
MyCoolObject()
To add on to Eduardo's answer, from everything I've read adding a _repr_html_ method should make iPython display the object when you type it into the console. I also use spyder though, and could not get it to work the way I expected. This simple wrapper class should allow you to display any html:
class displayedHTML:
def __init__(self, html):
self.html = html
def _repr_html_(self):
return self.html
But as you can see it does not work for me, instead showing the (implicitly defined) __repr__ of the class.
In [2]: obj = displayedHTML("<h1>" + "hello world" + "</h1>")
In [3]: obj
Out[3]: <__main__.displayedHTML at 0x1e8cda8f0d0>
I was not able to find the reason why this does not work, but I found a workaround if you just want to display a matplotlib colormap in the console from code (like I did).
Since know the matplotlib object works correctly, we can just give it to the ipython display function:
from IPython.display import display #Included without import since IPython 5.4 and 6.1
viridis = matplotlib.cm.get_cmap('viridis')
display(viridis)
And for me this works...
not_allowed_to_insert_pictures_yet.jpg
Hope this helps!

LICENSE.txt when loading data into tensorflow transfer learning

I am using code provided by tensorflow to load data: https://www.tensorflow.org/beta/tutorials/load_data/text
When I put in my own photos, it sends to a different directory. The code wants attributions from my LICENSE.txt, but I am not sure what the purpose of this code segment is.
I made my own LICENSE.txt file by just making a text file with each line being a title of an image. When I do this, it makes attributions a dictionary in which each key is the filename and each corresponding value is ''. When I run another method, I get a key error for every file.
import os
attributions = (data_root/"LICENSE.txt").open(encoding='utf- 8').readlines()
attributions = [line.split('\n') for line in attributions]
print(attributions)
attributions = dict(attributions)
import IPython.display as display
def caption_image(image_path):
image_rel = pathlib.Path(image_path).relative_to(data_root)
return "Image (CC BY 2.0) " + ' -'.join(attributions[str(image_rel)].split(' - ')[:-1])
for n in range(3):
image_path = random.choice(all_image_paths)
display.display(display.Image(image_path))
print(caption_image(image_path))
print()
I do not really know what to expect when I run the for loop in jupyter notebook, but it gives me a key error, the key being the file name.
I wrote that tutorial. The license lookup is only there so we can directly arttribute the individual photographers when we publish it. If you're working with your own images you don't need that part of the code at all.
All it's really doing is choosing a random image and displaying it. You can simplify it to:
import os
import IPython.display as display
for n in range(3):
image_path = random.choice(all_image_paths)
display.display(display.Image(image_path))

Jupyter notebook matplotlib figures missing in exported pdf

When generating a pdf in jupyter notebook, everything works great, but I would like to keep the inline figures inside the pdf, as well as the notebook.
Here is my code:
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('png', 'pdf')
save_figures = True
x = np.arange(0, 20, 0.1)
y = np.sin(x)
plt.figure()
plt.plot(x, y)
if save_figures:
plt.savefig('test.png')
plt.show()
The figures appear inline, but in the pdf what is printed instead is:
<IPython.core.display.Javascript object>
<IPython.core.display.HTML object>
The same thing appears in the pdf if I export from the web or use nbconvert to export to pdf from the command line.
Are there any additional commands that I need to invoke in order to get this to work?
If you change the %matplotlib notebook to %matplotlib inline, then PDF export with jupyter's nbconvert works fine for me. The %matplotlib notebook magic renders the plot in an interactive way that I suspect isn't properly recognized by LaTeX, which is used during the PDF conversion.
Alternatively, if you have to use %matplotlib notebook for some reason, the export to HTML with jupyter's nbconvert (jupyter nbconvert --to='html' Test.ipynb) seems to preserve the plot. I am then able to print this HTML file to a PDF from a web browser and the plot is present in the final PDF.
Crude solution to interactive problem:
in interactive function save figure to a file
fig.savefig("pieCharts.png")
in next cell display a file:
from IPython.display import Image
Image("pieCharts.png")
In PDF only output of the 2nd line will be displayed with image as changed in interactive function.