Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: 

 

# Copyright 2015 Daniel Schadt 

# Copyright 2016-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org> 

# 

# This file is part of qutebrowser. 

# 

# qutebrowser is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# qutebrowser is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. 

 

"""pdf.js integration for qutebrowser.""" 

 

import os 

 

from PyQt5.QtCore import QUrl 

 

from qutebrowser.utils import utils, javascript 

 

 

class PDFJSNotFound(Exception): 

 

"""Raised when no pdf.js installation is found. 

 

Attributes: 

path: path of the file that was requested but not found. 

""" 

 

def __init__(self, path): 

self.path = path 

message = "Path '{}' not found".format(path) 

super().__init__(message) 

 

 

def generate_pdfjs_page(url): 

"""Return the html content of a page that displays url with pdfjs. 

 

Returns a string. 

 

Args: 

url: The url of the pdf as QUrl. 

""" 

viewer = get_pdfjs_res('web/viewer.html').decode('utf-8') 

script = _generate_pdfjs_script(url) 

html_page = viewer.replace('</body>', 

'</body><script>{}</script>'.format(script)) 

return html_page 

 

 

def _generate_pdfjs_script(url): 

"""Generate the script that shows the pdf with pdf.js. 

 

Args: 

url: The url of the pdf page as QUrl. 

""" 

return ( 

'document.addEventListener("DOMContentLoaded", function() {{\n' 

' PDFJS.verbosity = PDFJS.VERBOSITY_LEVELS.info;\n' 

' (window.PDFView || window.PDFViewerApplication).open("{url}");\n' 

'}});\n' 

).format(url=javascript.string_escape(url.toString(QUrl.FullyEncoded))) 

 

 

def fix_urls(asset): 

"""Take an html page and replace each relative URL with an absolute. 

 

This is specialized for pdf.js files and not a general purpose function. 

 

Args: 

asset: js file or html page as string. 

""" 

new_urls = [ 

('viewer.css', 'qute://pdfjs/web/viewer.css'), 

('compatibility.js', 'qute://pdfjs/web/compatibility.js'), 

('locale/locale.properties', 

'qute://pdfjs/web/locale/locale.properties'), 

('l10n.js', 'qute://pdfjs/web/l10n.js'), 

('../build/pdf.js', 'qute://pdfjs/build/pdf.js'), 

('debugger.js', 'qute://pdfjs/web/debugger.js'), 

('viewer.js', 'qute://pdfjs/web/viewer.js'), 

('compressed.tracemonkey-pldi-09.pdf', ''), 

('./images/', 'qute://pdfjs/web/images/'), 

('../build/pdf.worker.js', 'qute://pdfjs/build/pdf.worker.js'), 

('../web/cmaps/', 'qute://pdfjs/web/cmaps/'), 

] 

for original, new in new_urls: 

asset = asset.replace(original, new) 

return asset 

 

 

SYSTEM_PDFJS_PATHS = [ 

# Debian pdf.js-common 

# Arch Linux pdfjs (AUR) 

'/usr/share/pdf.js/', 

# Arch Linux pdf.js (AUR) 

'/usr/share/javascript/pdf.js/', 

# Debian libjs-pdf 

'/usr/share/javascript/pdf/', 

# fallback 

os.path.expanduser('~/.local/share/qutebrowser/pdfjs/'), 

] 

 

 

def get_pdfjs_res_and_path(path): 

"""Get a pdf.js resource in binary format. 

 

Returns a (content, path) tuple, where content is the file content and path 

is the path where the file was found. If path is None, the bundled version 

was used. 

 

Args: 

path: The path inside the pdfjs directory. 

""" 

path = path.lstrip('/') 

content = None 

file_path = None 

 

# First try a system wide installation 

# System installations might strip off the 'build/' or 'web/' prefixes. 

# qute expects them, so we need to adjust for it. 

names_to_try = [path, _remove_prefix(path)] 

for system_path in SYSTEM_PDFJS_PATHS: 

content, file_path = _read_from_system(system_path, names_to_try) 

if content is not None: 

break 

 

# Fallback to bundled pdf.js 

if content is None: 

res_path = '3rdparty/pdfjs/{}'.format(path) 

try: 

content = utils.read_file(res_path, binary=True) 

except FileNotFoundError: 

raise PDFJSNotFound(path) from None 

 

try: 

# Might be script/html or might be binary 

text_content = content.decode('utf-8') 

except UnicodeDecodeError: 

return (content, file_path) 

text_content = fix_urls(text_content) 

return (text_content.encode('utf-8'), file_path) 

 

 

def get_pdfjs_res(path): 

"""Get a pdf.js resource in binary format. 

 

Args: 

path: The path inside the pdfjs directory. 

""" 

content, _path = get_pdfjs_res_and_path(path) 

return content 

 

 

def _remove_prefix(path): 

"""Remove the web/ or build/ prefix of a pdfjs-file-path. 

 

Args: 

path: Path as string where the prefix should be stripped off. 

""" 

prefixes = {'web/', 'build/'} 

if any(path.startswith(prefix) for prefix in prefixes): 

return path.split('/', maxsplit=1)[1] 

# Return the unchanged path if no prefix is found 

return path 

 

 

def _read_from_system(system_path, names): 

"""Try to read a file with one of the given names in system_path. 

 

Returns a (content, path) tuple, where the path is the filepath that was 

used. 

 

Each file in names is considered equal, the first file that is found 

is read and its binary content returned. 

 

Returns (None, None) if no file could be found 

 

Args: 

system_path: The folder where the file should be searched. 

names: List of possible file names. 

""" 

for name in names: 

try: 

full_path = os.path.join(system_path, name) 

with open(full_path, 'rb') as f: 

return (f.read(), full_path) 

except OSError: 

continue 

return (None, None) 

 

 

def is_available(): 

"""Return true if a pdfjs installation is available.""" 

try: 

get_pdfjs_res('build/pdf.js') 

except PDFJSNotFound: 

return False 

else: 

return True