Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to set HTTP headers #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/components/PDFData.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ const log = debug('app:components/PDFData');

import range from 'lodash/range';

function getDocument(url) {
// Using import statement in this way allows Webpack
// to treat pdf.js as an async dependency so we can
// avoid adding it to one of the main bundles
function getDocument(url, httpHeaders) {

// Using import statement in this way allows Webpack
// to treat pdf.js as an async dependency so we can
// avoid adding it to one of the main bundles

if (httpHeaders) {
return import(
/* webpackChunkName: 'pdfjs-dist' */
'pdfjs-dist/webpack').then(pdfjs => pdfjs.getDocument({
url: url,
httpHeaders: httpHeaders,
}));
}
return import(
/* webpackChunkName: 'pdfjs-dist' */
'pdfjs-dist/webpack').then(pdfjs => pdfjs.getDocument(url));
Expand Down Expand Up @@ -40,6 +50,10 @@ export default {
type: String,
required: true,
},
httpHeaders: {
type: Object,
required: false,
},
},

data() {
Expand All @@ -51,7 +65,7 @@ export default {
watch: {
url: {
handler(url) {
getDocument(url)
getDocument(url, this.httpHeaders)
.then(pdf => (this.pdf = pdf))
.catch(response => {
this.$emit('document-errored', {text: 'Failed to retrieve PDF', response});
Expand Down
6 changes: 5 additions & 1 deletion src/components/PDFDocument.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@pages-reset="fitWidth"
>
<PDFPage
v-bind="{scale, optimalScale, page, isPageFocused, isElementFocused}"
v-bind="{scale, optimalScale, page, isPageFocused, isElementFocused, selections}"
@page-rendered="onPageRendered"
@page-errored="onPageErrored"
@page-focus="onPageFocused"
Expand Down Expand Up @@ -60,6 +60,10 @@ export default {
isPreviewEnabled: {
default: false,
},
selections: {
type: Array,
default: () => []
},
},

computed: {
Expand Down
91 changes: 89 additions & 2 deletions src/components/PDFPage.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<template>
<canvas v-visible.once="drawPage" v-bind="canvasAttrs"></canvas>
<div>
<canvas ref="canvas" v-visible.once="drawPage" v-bind="canvasAttrs"></canvas>
<div ref="textLayer" class="textLayer"></div>
</div>
</template>

<script>
Expand All @@ -8,6 +11,10 @@ const log = debug('app:components/PDFPage');

import {PIXEL_RATIO} from '../utils/constants';
import visible from '../directives/visible';
import { TextLayerBuilder } from 'pdfjs-dist/web/pdf_viewer'
import {VIEWPORT_RATIO} from "../../../../../../pdf-viewer/utils/constants";
import 'pdfjs-dist/web/pdf_viewer.css'
import findAndReplaceDOMText from '../directives/findAndReplaceDOMText'

export default {
name: 'PDFPage',
Expand All @@ -33,6 +40,10 @@ export default {
type: Boolean,
default: false,
},
selections: {
type: Array,
default: () => []
}
},

directives: {
Expand Down Expand Up @@ -79,8 +90,9 @@ export default {
drawPage() {
if (this.renderTask) return;

const self = this;
const {viewport} = this;
const canvasContext = this.$el.getContext('2d');
const canvasContext = this.$refs.canvas.getContext('2d');
const renderContext = {canvasContext, viewport};

// PDFPageProxy#render
Expand All @@ -101,6 +113,81 @@ export default {
text: `Failed to render page ${this.pageNumber}`,
});
});

this.page.getTextContent().then((textContent) => {

// console.log(textContent);

// Set the layer position
const pdfCanvas = self.$refs.canvas;
const canvasTop = pdfCanvas.offsetTop;
const canvasLeft = pdfCanvas.offsetLeft;
const canvasHeight = pdfCanvas.offsetHeight;
const canvasWidth = pdfCanvas.offsetWidth;

const textLayerDiv = self.$refs.textLayer;
textLayerDiv.style.top = canvasTop + 'px';
textLayerDiv.style.left = canvasLeft + 'px';
textLayerDiv.style.width = canvasWidth + 'px';
textLayerDiv.style.height = canvasHeight + 'px';

// Scale text
const defaultViewport = self.page.getViewport(1);
const newScale = canvasWidth / (defaultViewport.width * VIEWPORT_RATIO);
const newViewport = self.page.getViewport(newScale);

// Create new instance of TextLayerBuilder class
const textLayer = new TextLayerBuilder({
textLayerDiv: self.$refs.textLayer,
pageIndex: self.page.pageIndex,
viewport: newViewport,
enhanceTextSelection: true,
});

// Set text-fragments
textLayer.setTextContent(textContent);

// Render text-fragments
textLayer.render();

// Highlight text
if (self.selections.length > 0) {

const observer = new MutationObserver(function (mutations) {

if (self.$refs.textLayer.children.length > 0) {

for (let i=0; i<self.selections.length; i++) {
if (self.page.pageNumber === self.selections[i].page) {

const color = self.selections[i].color;
const selection = self.selections[i].text
.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
.replace(/(\r\n|\n|\r|\s|[\u00a0])+/g, '(\\r\\n|\\n|\\r|\\s|[\\u00a0])+');
const wrapClass = 'css' + color.replace('#', '-');

findAndReplaceDOMText(self.$refs.textLayer, {
find: new RegExp(selection, 'gi'),
wrap: 'em',
wrapClass: wrapClass,
});

$('.' + wrapClass).css('background-color', color);
}
}

observer.disconnect();
}
});

observer.observe(self.$refs.textLayer, {
attributes: false,
childList: true,
characterData: false,
subtree: true
});
}
});
},

updateVisibility() {
Expand Down
8 changes: 7 additions & 1 deletion src/components/PDFViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<PDFData
class="pdf-viewer__main"
:url="url"
:httpHeaders="httpHeaders"
@page-count="updatePageCount"
@page-focus="updateCurrentPage"
@document-rendered="onDocumentRendered"
Expand All @@ -41,7 +42,7 @@
<PDFDocument
class="pdf-viewer__document"
:class="{ 'preview-enabled': isPreviewEnabled }"
v-bind="{pages, scale, optimalScale, fit, currentPage, pageCount, isPreviewEnabled}"
v-bind="{pages, scale, optimalScale, fit, currentPage, pageCount, isPreviewEnabled, selections}"
@scale-change="updateScale"
/>
</template>
Expand Down Expand Up @@ -77,6 +78,11 @@ export default {

props: {
url: String,
httpHeaders: Object,
selections: {
type: Array,
default: () => []
},
},

data() {
Expand Down
Loading