PDF export using pandoc and latex

This commit is contained in:
Olivier Keshavjee 2016-04-15 13:45:53 +02:00
parent 93c0e34779
commit b7dec65501
4 changed files with 60 additions and 25 deletions

View file

@ -1,15 +1,20 @@
#!/usr/bin/env python
# --!-- coding: utf8 --!--
import random
from PyQt5.QtCore import QUrl
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWebKitWidgets import QWebView
from PyQt5.QtWidgets import qApp
from manuskript.exporter.manuskript import HTML as MskHTML
from manuskript.exporter.pandoc.abstractOutput import abstractOutput
from manuskript.functions import tempFile
from manuskript.ui.views.PDFViewer import PDFViewer
class PDF(abstractOutput):
"""PDF Viewer using PDS.js. Cf. https://github.com/mozilla/pdf.js/wiki/Setup-PDF.js-in-a-website"""
name = "PDF"
description = qApp.translate("Export", "Needs latex to be installed.")
icon = "application-pdf"
@ -30,13 +35,12 @@ class PDF(abstractOutput):
return self.exporter.convert(src, args, outputfile)
def previewWidget(self):
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
# web.show()
return web
return PDFViewer()
def preview(self, settingsWidget, previewWidget):
filename = "/tmp/msk_asdlhadl.pdf"
filename = tempFile("msk_pdfpreview.pdf")
settingsWidget.writeSettings()
content = self.output(settingsWidget, outputfile=filename)
previewWidget.setUrl(QUrl("file://"+filename))
previewWidget.loadPDF(filename)

View file

@ -47,7 +47,7 @@ class abstractPlainText(markdown):
class pandocSetting:
def __init__(self, arg, type, format, label, widget=None, default=None, min=None, max=None, vals=None):
def __init__(self, arg, type, format, label, widget=None, default=None, min=None, max=None, vals=None, suffix=""):
self.arg = arg
self.type = type
self.label = label
@ -59,6 +59,7 @@ class pandocSetting:
self.min = min
self.max = max
self.vals = vals.split("|") if vals else []
self.suffix = suffix
def isValid(self, format):
"""Return whether the specific setting is active with the given format."""
@ -88,14 +89,14 @@ class pandocSettings(markdownSettings):
"TOC": pandocSetting("--toc", "checkbox", "",
qApp.translate("Export", "Include a table of contents.")),
"TOC-depth": pandocSetting("--toc-depth", "number", "",
"TOC-depth": pandocSetting("--toc-depth=", "number", "",
qApp.translate("Export", "Number of sections level to include in TOC: "),
default=3, min=1, max=6),
"smart": pandocSetting("--smart", "checkbox", "",
qApp.translate("Export", "Typographically correct output")),
"normalize": pandocSetting("--normalize", "checkbox", "",
qApp.translate("Export", "Normalize the document (cleaner)")),
"base-header": pandocSetting("--base-header-level", "number", "",
"base-header": pandocSetting("--base-header-level=", "number", "",
qApp.translate("Export", "Specify the base level for headers: "),
default=1, min=1),
@ -108,12 +109,31 @@ class pandocSettings(markdownSettings):
qApp.translate("Export", "Self-contained html files, with no dependencies")),
"q-tags": pandocSetting("--html-q-tags", "checkbox", "html",
qApp.translate("Export", "Use <q> tags for quotes in HTML")),
"latex-engine": pandocSetting("--latex-engine", "combo", "pdf",
"latex-engine": pandocSetting("--latex-engine=", "combo", "pdf",
qApp.translate("Export", "LaTeX engine used to produce the PDF."),
vals="pdflatex|lualatex|xelatex")
vals="pdflatex|lualatex|xelatex"),
}
pdfSettings = {
# PDF
"latex-ps": pandocSetting("--variable=papersize:", "combo", "pdf latex", # FIXME: does not work with default template
qApp.translate("Export", "Paper size:"),
vals="letter|A4|A5"),
"latex-fs": pandocSetting("--variable=fontsize:", "number", "pdf latex", # FIXME: does not work with default template
qApp.translate("Export", "Font size:"),
min=8, max=88, default=12, suffix="pt"),
"latex-class": pandocSetting("--variable=documentclass:", "combo", "pdf latex",
qApp.translate("Export", "Class:"),
vals="article|report|book|memoir"),
"latex-ls": pandocSetting("--variable=linestretch:", "combo", "pdf latex",
qApp.translate("Export", "Line spacing:"),
vals="1|1.25|1.5|2"),
# FIXME: complete with http://pandoc.org/README.html#variables-for-latex
}
def __init__(self, _format, toFormat=None, parent=None):
markdownSettings.__init__(self, _format, parent)
@ -142,6 +162,11 @@ class pandocSettings(markdownSettings):
self.addSettingsWidget("q-tags", self.grpPandocSpecific)
self.addSettingsWidget("latex-engine", self.grpPandocSpecific)
# PDF settings
self.settingsList.update(self.pdfSettings)
for i in self.pdfSettings:
self.addSettingsWidget(i, self.grpPandocSpecific)
self.toolBox.insertItem(self.toolBox.count() - 1, w, "Pandoc")
self.toolBox.layout().setSpacing(0) # Not sure why this is needed, but hey...
@ -181,6 +206,8 @@ class pandocSettings(markdownSettings):
s.widget.setMinimum(s.min)
if s.max:
s.widget.setMaximum(s.max)
if s.suffix:
s.widget.setSuffix(s.suffix)
l.addWidget(s.widget, 2)
parent.layout().addLayout(l)
@ -218,7 +245,6 @@ class pandocSettings(markdownSettings):
def getSettings(self):
self.settings = markdownSettings.getSettings(self)
# self.settings["Preview"]["MarkdownHighlighter"] = self.chkMarkdownHighlighter.isChecked()
P = self.settings.get("Pandoc", {})
@ -245,18 +271,22 @@ class pandocSettings(markdownSettings):
s = self.settingsList[name]
if s.isValid(self.format):
rr = ""
if s.type == "checkbox":
if s.widget.isChecked():
r.append(s.arg)
rr = s.arg
elif s.type == "number":
r.append("{}={}".format(
rr = "{}{}".format(
s.arg,
str(s.widget.value())
))
)
elif s.type == "combo":
r.append("{}={}".format(
rr = "{}{}".format(
s.arg,
s.widget.currentText()
))
)
if rr:
r.append(rr+s.suffix)
return r

View file

@ -5,17 +5,13 @@ import os
import re
from random import *
from PyQt5.QtCore import Qt, QRect, QStandardPaths, QObject, QRegExp
# Used to detect multiple connections
from PyQt5.QtGui import QBrush, QIcon, QPainter
from PyQt5.QtGui import QColor
from PyQt5.QtGui import QImage
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QRect, QStandardPaths, QObject, QRegExp, QDir
from PyQt5.QtGui import QBrush, QIcon, QPainter, QColor, QImage, QPixmap
from PyQt5.QtWidgets import qApp, QTextEdit
from manuskript.enums import Outline
# Used to detect multiple connections
AUC = Qt.AutoConnection | Qt.UniqueConnection
MW = None
@ -196,6 +192,10 @@ def allPaths(suffix=None):
paths.append(writablePath(suffix))
return paths
def tempFile(name):
"Returns a temp file."
return os.path.join(QDir.tempPath(), name)
def lightBlue():
"""

View file

@ -135,6 +135,7 @@ class exporterSettings(QWidget, Ui_exporterSettings):
self.getSettings()
def writeSettings(self):
self.getSettings()
with open(self.getSettingsPath(), 'w') as f:
# json.dumps(json.loads(json.dumps(allSettings)), indent=4, sort_keys=True)
json.dump(self.settings, f, indent=4, sort_keys=True)