0

Currently inside my html page I change some values, the way it works is like this:

The function:

function change1(){
   var myNewTitle = document.getElementById('myTextField1').value;
   if( myNewTitle.length==0 ){
       alert('Write Some real Text please.');
   return;
   }
   var titles = document.getElementsByClassName('title1');
   Array.prototype.forEach.call(titles,title => {
    title.innerHTML = myNewTitle;
   });
}

The values that get changed and the textfield used for input:

      Voornaam: <h3 class="title1">Kevin</h3>
      <input type="text" id="myTextField1"/>
      <input type="submit" id="byBtn" value="Change" onclick="change1()"/><br/>

      <p class="title1">Test.</p>

And I convert using wkhtmltopdf, which is a command line tool for html to PDF conversion. However the changes I make are ofcourse not done on the server but locally, and this is why my changes do not show in my conversion.

I was thinking about downloading the changed html page first and then convert it with wkhtmltopdf. Would anybody be able to give me an example of how this could be done or perhaps some me another and better way?

Kevin Bosman
  • 100
  • 11

1 Answers1

1

You have to send over the page content and the wkhtmltopdfon the server will do the job.

On the client side you need to all a function like this:

function send(){ 
  var f = document.createElement("form"),
     i = document.createElement("input"); //input element, hidden

  f.setAttribute('method',"post");
  f.setAttribute('action',"http://yourserver.com/script.php");

  i.setAttribute('type',"hidden");
  i.setAttribute('name',"content");
  i.setAttribute('value', document.body.innerHTML); // or the container you need

  f.appendChild(i);
  document.getElementsByTagName('body')[0].appendChild(f);
  f.submit();
};

On the server side, in php for example, you will get the content, save it on temp file and then call the tool to convert it to pdf and then return the pdf to the client:

<?php
try {
   $content = $_REQUEST['content'];
    if(!file_exists('/tmp') ){
       mkdir('/tmp', 0777);
    }
   $fp = fopen('w+','/tmp/tmp.html');
   if($fp){
     fwrite($fp, $content);
     fclose($fp);

     $filename = '/tmp/out_' . time() .'.pdf'; // output filename
     shell_exec('wkhtmltopdf /tmp/tmp.html ' . $filename);

     //then eventually ask user for download the result
     header("Content-type:application/pdf");

     // It will be called output.pdf
     header("Content-Disposition:attachment;filename='output.pdf'");

    readfile($filename);
  }else{
     echo 'html file could not be created';
  }
} catch (Exception $e) {
  echo 'exception: ',  $e->getMessage(), "\n";
}
bluehipy
  • 2,254
  • 1
  • 13
  • 19
  • Thank you for this great explanation, I will be trying it out very shortly. – Kevin Bosman Jun 02 '17 at 10:48
  • Your code seems to work, It downloads output.pdf for me. However when opening the pdf it tells me failed to open pdf file. – Kevin Bosman Jun 02 '17 at 11:44
  • @KevinBosman try first to open the tmp.html and check if it contains whait it should also check the generated pdfs in tmp folder. – bluehipy Jun 02 '17 at 11:57
  • How would I go opening them, using fopen($fp); ? Excuse me for some questions as I am not very experienced. Maybe it could be a problem that content is not being read due to me running the code you provided in a seperate file? – Kevin Bosman Jun 02 '17 at 12:12
  • There are some errors I receive when I put them in the same file, such as otice: Undefined index: content, Cannot modify header information - headers already sent by (it says line 183 which is where I basically just include a footer). and Warning: readfile(/tmp/out_1496406087.pdf): failed to open stream: No such file or directory.. which seems to be a big problem. So I assume these are the errors I don't get shown when putting it in a different file. Yet they do occur. – Kevin Bosman Jun 02 '17 at 12:23
  • @KevinBosman code should be in separate files. The file script.php should vontain only the php code. By the way, skip the ending "?>" You have to do some debuging. Check what is sent on the server from the client. My code is just demonstrating the basics :) – bluehipy Jun 02 '17 at 12:29
  • Alright, will do. By the way, also found a little mistake in your code I think. Check where you define params=. – Kevin Bosman Jun 02 '17 at 12:30
  • Just a heads up, I succesfully run my function now and it runs my script.php aswell. It gets to the part where it shows an alert. So Im gonna try to figure that out, any tips or ideas are welcome. Also, Im accepting your answer. – Kevin Bosman Jun 02 '17 at 13:14
  • @KevinBosman That's goid. It neans it works. Remove the alert from the onready handler. Is the pdf downloading? – bluehipy Jun 02 '17 at 13:16
  • No the pdf is unfortunately not downloading at the moment. Will remove that alert whenever it is downloading. The alert it gives me is a warning showing a failed to open stream error. Based on the readfile($filename); – Kevin Bosman Jun 02 '17 at 13:21
  • You have a folder called tmp with permission for writing on it right? – bluehipy Jun 02 '17 at 13:45
  • Update php code with what I just updated, It might help. – bluehipy Jun 02 '17 at 13:52
  • I have created a tmp folder in the same map as my index and script.php, if that's what you mean.. otherwise I'm not really sure. Could this also be done with another folder I have called pdf-quotes, that's the map I used before and the pdf files got sent there successfully. – Kevin Bosman Jun 02 '17 at 13:56
  • Using the updated php, On line 5 it says unexpected ; once that is resolved it givs me unexpected $fp (T_VARIABLE) on line 7. Which obviously appears becuase of the removed ;. I cant seem to figure out why the ; is unexpected as your code seems fine to me. – Kevin Bosman Jun 02 '17 at 14:01
  • Updated again :) – bluehipy Jun 02 '17 at 14:50
  • Excuse me not replying for a bit, had a weekend off. It gives me a notice: undefined index: content on line 3 and a warning Warning: fopen(w+): failed to open stream: Success in [path] on line 7 html file could not be created. – Kevin Bosman Jun 06 '17 at 06:41
  • Little mistake: the undefined index notice is not there if I run it from the right html page. Although it does tell me fopen(w+): failed to open stream. and html file could not be created. – Kevin Bosman Jun 06 '17 at 06:52
  • The fopen arguments required to be the other way around, now I get an alert including things such as PDF1.4, title, creator.. it does not download. – Kevin Bosman Jun 06 '17 at 07:24
  • @KevinBosman the tmp path is not writable it seems. Delete the tmp you have created and let the script make it for you. – bluehipy Jun 06 '17 at 07:24
  • Deleted that tmp, still the same as above. An alert containing some things like PDF1.4, title, creator. – Kevin Bosman Jun 06 '17 at 07:25
  • I assume it is the pdf content, not an error anymore. Not sure whether it is the content since it is quite weird. But it does not give me an error anywhere. Also in the creator part it tells me wkhtmltopdf. I assume that's correct, since that is what I use. – Kevin Bosman Jun 06 '17 at 07:32
  • @KevinBosman that is ok, delete the alert(http.responseText); – bluehipy Jun 06 '17 at 07:33
  • I could do this, although it does not download for me yet. And delete the entire if where it shows the alert or just the alert? When I comment out the alert, it gives me nothing. Nothing happens, no download or anything. – Kevin Bosman Jun 06 '17 at 07:35
  • @KevinBosman it looks limetheajaxcall has to beset toe pect a binary response. Si if you eant a ajax call, read more here: https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post or switch to a form submit rather then a ajax call. – bluehipy Jun 06 '17 at 08:11
  • Well, I will take a look at that. Thank you for all the help. – Kevin Bosman Jun 06 '17 at 08:17
  • @KevinBosman i llpost the solution with form but niw i am on a phone and is a pain to format cide :)) – bluehipy Jun 06 '17 at 08:34
  • hahah that is alright and very understandable, thank you for your effort! Im looking forward to seeing the solution. – Kevin Bosman Jun 06 '17 at 08:40
  • Could you perhaps tell me when you're able to help me further with the example? – Kevin Bosman Jun 06 '17 at 10:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145953/discussion-between-bluehipy-and-kevin-bosman). – bluehipy Jun 06 '17 at 10:08
  • I have sent you a message in chat, if you perhaps have the time for this. – Kevin Bosman Jun 08 '17 at 07:11