Skip to content

Commit

Permalink
Parsing pdf and copy result
Browse files Browse the repository at this point in the history
  • Loading branch information
a-jaillet authored and Embraser01 committed Feb 8, 2021
1 parent ca9bfa1 commit fea0590
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { Component, Input, OnInit } from '@angular/core';
import { Component, Input } from '@angular/core';

@Component({
selector: 'invoice-tool-invoice-article',
templateUrl: './invoice-article.component.html',
})
export class InvoiceArticle implements OnInit {
export class InvoiceArticle {

@Input() articlename: String;
@Input() articlename: string;
@Input() nbarticle: number;
constructor() { }

ngOnInit() {
}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Component, Input, OnInit } from '@angular/core';
import { Component, Input } from '@angular/core';

@Component({
selector: 'invoice-tool-invoice-file',
templateUrl: './invoice-file.component.html',
})
export class InvoiceFile implements OnInit {
export class InvoiceFile {

@Input() invoicename: string;
constructor() { }

ngOnInit() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ <h1>Outil de gestion des factures</h1>
</a>
</mat-card-header>
<mat-card-content>
<section class="mat-typography">
<h1 style="margin: 17px">Entre tes factures :</h1>
</section>
<div id="dropzone"
(dragover)="false"
(dragend)="false"
(drop)="onDrop($event)">
<p style="margin: 10px; text-align: center">
<strong>Pose tes fichier pdf ici</strong>
<mat-progress-bar mode="indeterminate" *ngIf="isLoading"></mat-progress-bar>
</p>
<p style="margin: 10px">
<strong>Tes factures prises en comptes :</strong>
Expand All @@ -34,18 +30,23 @@ <h1 style="margin: 17px">Entre tes factures :</h1>
<p></p>
<div fxLayout="column" fxFlexAlign="center" fxLayoutGap="20px">
<div fxFlexAlign="center" fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="40px">
<button mat-stroked-button (click) = "onSubmit()">Lancer le calcul</button>
<button mat-stroked-button (click) = "onSubmit()"
[disabled]="invoices.length===0">Lancer le calcul</button>
</div>
</div>

<section class="mat-typography" *ngIf="!isEmpty">
<h1>Résultat :</h1>
</section>
<p style="margin: 30px">
<invoice-tool-invoice-article *ngFor="let article of articles"
[articlename]="article[0]"
[nbarticle]="article[1]"> </invoice-tool-invoice-article>
</p>
</mat-card-content>
</mat-card>
<p></p>
<mat-progress-bar mode="indeterminate" *ngIf="isLoading"></mat-progress-bar>
<div fxLayout="column" fxFlexAlign="center" fxLayoutGap="20px" *ngIf="articles.length>0">
<div fxFlexAlign="center" fxLayout="row" fxLayout.xs="column" fxLayoutGap.gt-xs="40px">
<button mat-stroked-button (click) = "onPrint()" >Afficher le résultat</button>
<button mat-stroked-button (click) = "onCopyData()" >Copier le résultat</button>
</div>
</div>
<p style="margin: 30px" *ngIf="isPrint">
<invoice-tool-invoice-article *ngFor="let article of articles"
[articlename]="article[0]"
[nbarticle]="article[1]"> </invoice-tool-invoice-article>
</p>
</mat-card-content>
</mat-card>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,84 @@ import { Subscription } from 'rxjs';
}
`]
})

export class InvoiceParse implements OnInit {

invoices: File[];
articles: any[];
isEmpty: boolean;
isLoading: boolean;
isPrint: boolean;


invoiceSubscription: Subscription;
constructor(private invoiceService: InvoiceService,
private invoiceParseService: InvoiceParseService) { }

ngOnInit() {
ngOnInit(): void {
this.invoiceSubscription = this.invoiceService.invoicesSubject.subscribe(
(invoices: any[]) => {
(invoices: File[]) => {
this.invoices = invoices;
}
);
this.invoiceService.emitAppareilSubject();
this.isEmpty = true;
this.isLoading = false;
this.articles = [];
this.isPrint = false;
}

onDrop(e) {
e.preventDefault(); //<span spellcheck="true">;// évite d'ouvrir le fichier recherché</span>
onDrop(e: any): void {
e.preventDefault();
const files: File = e.dataTransfer.files;
Object.keys(files).forEach((key) => {
const file: File = files[key];
this.invoiceParseService.fromFiletoText(file);
this.invoiceService.addInvoice(file);
if(file.type === "application/pdf"){
this.invoiceService.addInvoice(file);
}
});
}

onRemove() {
onRemove(): void {
this.invoiceService.removeInvoice();
this.invoiceParseService.removeOne();
}

async onSubmit() {
if(this.invoices.length <= 0){
alert("Vous devez rentrer au moins une facture");
async onSubmit(): Promise<void>{

this.isLoading = true;
for(let i=0; i<this.invoices.length; i++){
await this.invoiceParseService.fromFiletoText(this.invoices[i]);
}
else{
this.invoiceParseService.parsePDF();
this.articles = this.invoiceParseService.listarticle;
this.isEmpty = false;
this.invoiceService.removeAll();
this.invoiceParseService.parsePDF();
this.articles = this.invoiceParseService.listarticle;
this.invoiceService.removeAll();

this.isLoading = false;
return Promise.resolve();
}

onPrint(): void{
this.isPrint = !this.isPrint;
}




onCopyData(): void{
let tocopy = '';
for(let i =0 ; i<this.articles.length ; i++){
tocopy += this.articles[i][0] + ';' + this.articles[i][1] + '\n';
}

const selBox = document.createElement('textarea');
selBox.style.position = 'fixed';
selBox.style.left = '0';
selBox.style.top = '0';
selBox.style.opacity = '0';
selBox.value = tocopy;
document.body.appendChild(selBox);
selBox.focus();
selBox.select();
document.execCommand('copy');
document.body.removeChild(selBox);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export class InvoiceParseService {
textinvoices = [];
listarticle = [];
nbplastinvoice = 0;
async fromFiletoText(invoice: File){



async fromFiletoText(invoice: File): Promise<void> {

const url = URL.createObjectURL(invoice);
const oneinvoice = await this.gettext(url);
Expand All @@ -18,9 +21,10 @@ export class InvoiceParseService {
this.textinvoices.push(oneinvoice[i]);
}
URL.revokeObjectURL(url);
return Promise.resolve();
}

parsePDF() {
parsePDF(): void {

const listinvoices = this.textinvoices;
const listothers = [];
Expand Down Expand Up @@ -136,28 +140,21 @@ export class InvoiceParseService {

}

removeOne(){
for(let i=0;i<this.nbplastinvoice;i++){
this.textinvoices.pop();
}
}



gettext(pdfUrl){
async gettext(pdfUrl: string): Promise<string[]> {
PDFJS.GlobalWorkerOptions.workerSrc = PDFJSWorker;
const pdf = PDFJS.getDocument(pdfUrl).promise;
return pdf.then(function(pdf) { // get all pages text
return pdf.then(function(pdf: { numPages: number; getPage: (arg0: number) => any}) { // get all pages text
const maxPages = pdf.numPages;
const countPromises = []; // collecting all page promises
for (let j = 1; j <= maxPages; j++) {
const page = pdf.getPage(j);

//var txt = "";
countPromises.push(page.then(function(page) { // add page promise
countPromises.push(page.then(function(page: { getTextContent: () => any}) { // add page promise
const textContent = page.getTextContent();
return textContent.then(function(text){ // return content promise
return text.items.map(function (s) { return s.str; }).join(''); // value page text
return textContent.then(function(text: { items: any[]}){ // return content promise
return text.items.map(function (s: { str: any}) { return s.str; }).join(''); // value page text
});
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ import { Injectable } from '@angular/core';

export class InvoiceService {

invoicesSubject = new Subject<any[]>();
private invoices = [];
invoicesSubject = new Subject<File[]>();
private invoices: File[];

emitAppareilSubject() {
constructor() {
this.invoices = [];
}

emitAppareilSubject(): void {
this.invoicesSubject.next(this.invoices.slice());
}

addInvoice(file : File) {
addInvoice(file: File): void {
const invoiceObject = file;
this.invoices.push(invoiceObject);
this.emitAppareilSubject();
}

removeInvoice() {
removeInvoice(): void {
this.invoices.pop();
this.emitAppareilSubject();
}

removeAll() {
removeAll(): void {
this.invoices = [];
this.emitAppareilSubject();
}
Expand Down

0 comments on commit fea0590

Please sign in to comment.