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

Try to get multitple pdfs from server , then do all file merged #3

Closed
hoihoi000a opened this issue Oct 17, 2018 · 12 comments
Closed

Try to get multitple pdfs from server , then do all file merged #3

hoihoi000a opened this issue Oct 17, 2018 · 12 comments

Comments

@hoihoi000a
Copy link

i am trying to get file from server with javascript
according ur demo
i am using XMLHttpRequest to get pdf file,then do the merge process,but i couldn't
do you have any advice for this?
thank you :)

@TakeshiOkamoto
Copy link
Owner

TakeshiOkamoto commented Oct 17, 2018

Hi, hoihoi000a

I can English understand, only 3-year-old level.

[Precondition]
PDF Designer's core was created in 2003.
So, Basically it corresponds to PDF 1.2 - 1.4.

[Method 1]
Set the data to be passed to TPDFAnalyst.LoadFromStream to Uint8Array.

[Method 2]
Save the data using HTML5 IndexedDB, then read with Uint8Array.

@TakeshiOkamoto
Copy link
Owner

if can't do it, sorry

@hoihoi000a
Copy link
Author

hoihoi000a commented Oct 17, 2018

hello TakeshiOkamoto,
Thanks for your support ! your library is amazing!! 👍
i simply the demo code below, it worked

function onChangeFile(event,index) {
    var reader = new FileReader();
    var file = event.target.files;
    reader.onload = function (event) {
        var `Stream` = new Uint8Array(reader.result);
        Analysts[index] = new TPDFAnalyst();
        try {
            Analysts[index].LoadFromStream(Stream);
        } catch (e) {
            Analysts[index] = null;
        }
    }
    reader.readAsArrayBuffer(file[0]);
}

//-----------------------------------------------------------------
//and i try to send file to unit8Array(),but still not working
// have been looking for ans all day, do you have any idea?
// sorry for bother you with small question
//-----------------------------------------------------------------

function loadXMLDoc() {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","a.pdf",true);
    xmlhttp.onload = function (event) {
        var arraybuffer = xmlhttp.response;
        var Stream = new Uint8Array(arraybuffer);
        Analysts[index] = new TPDFAnalyst();
        try {
            Analysts[index].LoadFromStream(Stream);
        } catch (e) {
            Analysts[index] = null;
        }
    }
    reader.readAsArrayBuffer(file[0]);
}

@TakeshiOkamoto
Copy link
Owner

I understood the cause.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data

I will post the code again after formatting the code.

@TakeshiOkamoto
Copy link
Owner

Early story,

xmlhttp.responseType = "arraybuffer";

It seems that it was missing.


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="pdf-designer.js"></script> 
<script> 
 
  function createXmlHttpRequest(){
 
    if(window.XMLHttpRequest){      
    
      return new XMLHttpRequest();     
      
    }else if(window.ActiveXObject){
      
      try {
          return new ActiveXObject("MSXML2.XMLHTTP.6.0"); 
      } catch (e) {
        try {
           return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        } catch (e2) {
          try {
              return new ActiveXObject("MSXML2.XMLHTTP");
          } catch (e3) {
             return null
          }
        }
      }
      
    }else {
        return null
    }
  }
  
  function sendHttpRequest(){ 
        
      var xmlhttp = createXmlHttpRequest();
      
      xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
            XmlHttpCallback(xmlhttp);
        }
      }       

      xmlhttp.open('GET', 'test.pdf', true);
            
      // here is important
      xmlhttp.responseType = "arraybuffer";
      

      xmlhttp.send(null);     
  }
  
  function XmlHttpCallback(xmlhttp){
      var result = document.getElementById("result");
      
      var Analysts = new TPDFAnalyst();
      var Stream = new Uint8Array(xmlhttp.response);
      try {
         Analysts.LoadFromStream(Stream);
      }catch (e){
        alert(e);
      }
      
      console.log(Analysts);
      result.innerHTML="PDF" + Analysts.Version + " PageCount : " + Analysts.PageCount;
  } 
</script>
</head>
<body>
 
  <div id="result"></div>
  <br />
  <input type="button" value="  run  " onclick="sendHttpRequest();" />
  
</body>
</html>

It should be okay with this.

... Translated by google translation.

@hoihoi000a
Copy link
Author

hoihoi000a commented Oct 18, 2018

どうもありがとう (translated by goolge)
thank you so much,TakeshiOkamoto
you help me a lot, i appreciate that 👍
ill do the research about your code and that links
thank you again :)
if that work i will copy my code to the comment

@hoihoi000a
Copy link
Author

hoihoi000a commented Oct 22, 2018

i cant even leaving a comment, so weird
seems website problem
the code can't be read by the website

i might do it few days later
var fileArray = <?php echo json_encode($fileArray); ?>; var arrlength = <?php echo $arrlength ;?>; var Analysts = new Array(); for(var i = 0 ; i<arrlength;i++){ sendHttpRequest(fileArray[i],i) }

@TakeshiOkamoto
Copy link
Owner

Probably because there is an HTML tag.

[All code]
4 single-byte spaces or tabs at the beginning of the line

And posting is good, after previewing.

I think that is good.


[Tag test]

<html>
</html>

[Example]
CRLF ... new line
SP ... space

CRLF
SPSPSPSP<html>
SPSPSPSP</html>

@hoihoi000a
Copy link
Author

hoihoi000a commented Oct 29, 2018

According to TakeshiOkamoto's code
I try to reuse that way get file with php then do the merge with js
I find the problem , the code mark doesn't work , unless 3 times of code mark.
Because i do a lot research for 2 weeks, and finally get the answer, and thanks to TakeshiOkamoto's help
Here is a simple sample for other learner.(I think it's working right away)


Precondition 1. u need "pdf-designer.js" from TakeshiOkamoto's code.
Precondition 2. change the filearray's file address from server.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
  <div id="errmsg"></div>
  <br />
  <input type="button" value=" combinePDF " onclick="combinePDF()" />
  <?php
    $fileArray = array("http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf","http://mozilla.github.io/pdf.js/web/compressed.tracemonkey-pldi-09.pdf");
    $arrlength = count($fileArray);
    echo $arrlength;
  ?>
</body>
</html>

<script src="pdf-designer.js"></script> 
<script> 
  var fileArray = <?php echo json_encode($fileArray); ?>;
  var arrlength = <?php echo $arrlength ;?>;
  var Analysts  = new Array();
  for(var i = 0 ; i<arrlength;i++){
      sendHttpRequest(fileArray[i],i)
    }
 
  function createXmlHttpRequest(){

    if(window.XMLHttpRequest){      
    
      return new XMLHttpRequest();     
      
    }else if(window.ActiveXObject){
      
      try {
          return new ActiveXObject("MSXML2.XMLHTTP.6.0"); 
      } catch (e) {
        try {
           return new ActiveXObject("MSXML2.XMLHTTP.3.0");
        } catch (e2) {
          try {
              return new ActiveXObject("MSXML2.XMLHTTP");
          } catch (e3) {
             return null
          }
        }
      }
      
    }else {
        return null
    }
  }

  function sendHttpRequest(filepath,index){ 
      var xmlhttp = createXmlHttpRequest();
      xmlhttp.open('GET', filepath, true);      
      xmlhttp.responseType = "arraybuffer";// here is important
      xmlhttp.send(null); 

      xmlhttp.onreadystatechange = function() { 
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
            Analysts[index] = new TPDFAnalyst();
            var Stream = new Uint8Array(xmlhttp.response);
            try {
              Analysts[index].LoadFromStream(Stream);
            }catch (e){
              alert(e);
            }   
        }
      }      
  }
  
  function combinePDF() {        
      var PDFCombine = new TPDFCombine();
    if (Analysts[0] != null && Analysts[1] != null) {
        try {
          
            // PDF_GetDateTime_Now() return the current date and time.
            PDFCombine.SaveToFile(PDF_GetDateTime_Now() + '.pdf', Analysts);
            
        } catch (e) {
            Analysts[0] = null;
            Analysts[1] = null;
            
            document.getElementById("errmsg").innerHTML = 
              'failed in the convert of the PDF file.';
        }
    } else {
        document.getElementById("errmsg").innerHTML =
          'Two PDF files, please select.';
    }
  }
</script>

@TakeshiOkamoto
Copy link
Owner

Thanks!

@hoihoi000a
Copy link
Author

Do i need to closed this issue?
i am new citizen, not quite understand how this website going,sorry @@

@TakeshiOkamoto
Copy link
Owner

It is OK to closed this "issue".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants