mirror of
https://github.com/tomru/pdfer.git
synced 2026-03-03 06:27:19 +01:00
no need for pdf-js
This commit is contained in:
15
webapp/apiHelper.js
Normal file
15
webapp/apiHelper.js
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
export function generatePdf(state){
|
||||||
|
return fetch('//localhost:5000/pdf/generate/brief', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(state)
|
||||||
|
}).then(function(res) {
|
||||||
|
if (res.status !== 200) {
|
||||||
|
console.log('request failed');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return res.json();
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -10,7 +10,5 @@
|
|||||||
<div id="app">Loading</div>
|
<div id="app">Loading</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch.min.js"></script>
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.2/fetch.min.js"></script>
|
||||||
<script src="/libs/pdf.js"></script>
|
|
||||||
<script src="/libs/pdf.worker.js"></script>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
43506
webapp/static/libs/pdf.worker.js
vendored
43506
webapp/static/libs/pdf.worker.js
vendored
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,7 @@ export default function(props) {
|
|||||||
return (
|
return (
|
||||||
<label>
|
<label>
|
||||||
{props.text}:
|
{props.text}:
|
||||||
<input onChange={props.onChange.bind(null, props.name)} type="text" />
|
<input onchange={props.onchange.bind(null, props.name)} type="text" />
|
||||||
</label>
|
</label>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,11 @@
|
|||||||
import { Component, h } from 'preact';
|
import { Component, h } from 'preact';
|
||||||
import PDF from 'react-pdf';
|
|
||||||
|
|
||||||
class Preview extends Component {
|
export default props => {
|
||||||
render(props) {
|
if (!props.pdfUrl) {
|
||||||
if (!props.pdfUrl) {
|
return '';
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<PDF
|
|
||||||
file={props.pdfUrl}
|
|
||||||
onDocumentComplete={this._onDocumentComplete.bind(this)}
|
|
||||||
onPageComplete={this._onPageComplete.bind(this)}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_onDocumentComplete(pages) {
|
return (
|
||||||
this.setState({pages: pages});
|
<embed src={props.pdfUrl} width="100%" height="100%"/>
|
||||||
}
|
);
|
||||||
|
};
|
||||||
_onPageComplete(page) {
|
|
||||||
this.setState({page});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default Preview;
|
|
||||||
|
|||||||
10
webapp/views/components/TextAreaInput.js
Normal file
10
webapp/views/components/TextAreaInput.js
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { h } from 'preact';
|
||||||
|
|
||||||
|
export default function(props) {
|
||||||
|
return (
|
||||||
|
<label>
|
||||||
|
{props.text}:
|
||||||
|
<textarea onchange={props.onchange.bind(null, props.name)} />
|
||||||
|
</label>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -2,21 +2,27 @@ import { Component, h } from 'preact';
|
|||||||
import PDF from 'react-pdf';
|
import PDF from 'react-pdf';
|
||||||
|
|
||||||
import Input from '../components/Input';
|
import Input from '../components/Input';
|
||||||
|
import TextAreaInput from '../components/TextAreaInput';
|
||||||
import Button from '../components/Button';
|
import Button from '../components/Button';
|
||||||
import Preview from '../components/Preview';
|
import Preview from '../components/Preview';
|
||||||
|
|
||||||
function toBase64(arrayBuffer) {
|
import {generatePdf} from '../../apiHelper';
|
||||||
return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
|
|
||||||
}
|
|
||||||
|
|
||||||
class Home extends Component {
|
class Home extends Component {
|
||||||
render(props) {
|
render(props) {
|
||||||
|
const component = this;
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
<TextAreaInput
|
||||||
|
name="address"
|
||||||
|
text="Adresse"
|
||||||
|
onchange={this._onChange.bind(this)}
|
||||||
|
value={props.address}
|
||||||
|
/>
|
||||||
<Input
|
<Input
|
||||||
name="subject"
|
name="subject"
|
||||||
text="Betreff"
|
text="Betreff"
|
||||||
onChange={this._onChange.bind(this)}
|
onchange={this._onChange.bind(this)}
|
||||||
value={props.subject}
|
value={props.subject}
|
||||||
/>
|
/>
|
||||||
<Button onClick={this._onGenerate.bind(this)} text="Generiere"/>
|
<Button onClick={this._onGenerate.bind(this)} text="Generiere"/>
|
||||||
@@ -25,6 +31,21 @@ class Home extends Component {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_onGenerate() {
|
||||||
|
generatePdf(this.state)
|
||||||
|
.then((data) => {
|
||||||
|
const {id} = data;
|
||||||
|
this.setState({
|
||||||
|
pdfUrl: `//localhost:5000/pdf/${id}`
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
this.setState({
|
||||||
|
pdfUrl: null
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
_onChange(name, event) {
|
_onChange(name, event) {
|
||||||
if (!name) {
|
if (!name) {
|
||||||
return;
|
return;
|
||||||
@@ -33,32 +54,6 @@ class Home extends Component {
|
|||||||
this.setState({[name]: value || null})
|
this.setState({[name]: value || null})
|
||||||
}
|
}
|
||||||
|
|
||||||
_onGenerate(name, event){
|
|
||||||
var component = this;
|
|
||||||
|
|
||||||
fetch('//localhost:5000/pdf/generate/brief', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify(this.state)
|
|
||||||
}).then(function(res) {
|
|
||||||
if (res.status !== 200) {
|
|
||||||
console.log('request failed');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return res.json();
|
|
||||||
}).then(function(data) {
|
|
||||||
const {id} = data;
|
|
||||||
component.setState({
|
|
||||||
pdfUrl: `//localhost:5000/pdf/${id}`
|
|
||||||
});
|
|
||||||
}).catch(function(error) {
|
|
||||||
component.setState({
|
|
||||||
pdfUrl: null
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Home;
|
export default Home;
|
||||||
|
|||||||
Reference in New Issue
Block a user