I have a program to write a text and when I click on the 'compile' button, it compiles to latex, converts to pdf and displays it on my application. The problem is that when I have a compilation error, the application bugs and that's it. I'd like to know if it's possible to recover compilation errors without the application crashing. I tried with try/expect but it's not a python error so it doesn't work.
You will need PDF.js to view the pdf https://mozilla.github.io/pdf.js/getting_started/#download
Ui :
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(841, 481)
self.horizontalLayout = QtWidgets.QHBoxLayout(Dialog)
self.horizontalLayout.setObjectName("horizontalLayout")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.textEdit = QtWidgets.QTextEdit(Dialog)
self.textEdit.setMinimumSize(QtCore.QSize(0, 200))
self.textEdit.setObjectName("textEdit")
self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.gridLayout.addWidget(self.buttonBox, 0, 1, 1, 1)
#self.textEdit_2 = QtWidgets.QTextEdit(Dialog)
#self.textEdit_2.setObjectName("textEdit_2")
#self.gridLayout.addWidget(self.textEdit_2, 1, 0, 1, 1)
self.horizontalLayout.addLayout(self.gridLayout)
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
app:
import os
import sys
from PyQt5.QtWidgets import QDialog, QPushButton, QWidget, QApplication
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
from sympy import Symbol
import untitled
x=Symbol('x')
class Test(QDialog, untitled.Ui_Dialog):
def __init__(self):
super(Test, self).__init__()
self.setupUi(self)
self.bouton= QPushButton('compile',self)
self.horizontalLayout.addWidget(self.bouton)
self.bouton.clicked.connect(self.crertest)
self.widget = QWidget(self)
self.gridLayout.addWidget(self.widget, 1, 0, 1, 1)
self.t = Window()
lay = QtWidgets.QVBoxLayout(self.widget)
lay.addWidget(self.t)
def crertest(self):
try :
def preambule(*packages):
p = ""
for i in packages:
p = p + "\\usepackage{" + i + "}\n"
return p
start = "\\documentclass[12pt,a4paper,french]{article}\n\\usepackage[utf8]{inputenc}\n"
start = start + preambule('amsmath','graphicx')
start = start + "\\begin{document}\n"
end = "\\end{document}"
body = self.textEdit.toPlainText()
container = start + body + end
file = "mypdf.tex"
if os.path.exists(file):
os.remove(file)
fichier = open("mypdf.tex", "x") #
fichier.write(container)
fichier.close()
instructions = "pdflatex " + file
os.system(instructions)
readpdf = "START " + file[:-4] + ".pdf"
self.t.loadd()
except:
print('Fail')
PDFJS = 'file:///C:/Users/pdf_js/web/viewer.html' #Path too viewer.htlm in your pddf_js folder
PDF = 'file:///C:/Users/mypdf.pdf' #Path to your pdf
class Window(QtWebEngineWidgets.QWebEngineView):
def __init__(self):
super(Window, self).__init__()
self.loadd()
try:
def loadd(self):
self.load(QtCore.QUrl.fromUserInput('%s?file=%s' % (PDFJS, PDF)))
except:
print ('Fail 2')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = Test()
main.show()
app.exec_()
I use the code taken from this post for the pdf display : https://stackoverflow.com/questions/23389001/how-to-render-pdf-using-pdf-js-viewer-in-pyqt
instructions = "pdflatex " + file
this line compile, but i can't catch error
os.system("pdflatex "+file). That means that you should be able to runpdflatex filefrom the command line and see what happens. Alsofile.logwill have the result either way. Are those enlightening? – Teepeemm Mar 03 '20 at 17:38os.systemyou can presumably call something likepdflatex --interaction=batchmode file || echo hmmm pdflatex had an errorthen the combination will never return a non zero error code: either pdflatex will succeed or the echo will succeed. – David Carlisle Mar 03 '20 at 20:34