Advertisement
nrzmalik

Save Pdf file JavaScript

Mar 28th, 2023 (edited)
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 1.11 KB | Source Code | 0 0
  1. // Get the value of the "Title" variable
  2. var title = GetPlayer().GetVar("Title");
  3.  
  4. // Get the value of the "Editor" variable
  5. var editor = GetPlayer().GetVar("Editor");
  6.  
  7. // Create a new jsPDF object
  8. var pdf = new jsPDF();
  9.  
  10. // Set the document name to the value of the "Title" variable
  11. pdf.setProperties({
  12. title: title
  13. });
  14.  
  15. // Calculate the width and height of the page
  16. var pageWidth = pdf.internal.pageSize.getWidth();
  17. var pageHeight = pdf.internal.pageSize.getHeight();
  18.  
  19. // Split the editor text into lines based on the width of the page
  20. var lines = pdf.splitTextToSize(editor, pageWidth - 20);
  21.  
  22. // Calculate the height of the lines
  23. var lineHeight = pdf.getTextDimensions(lines[0]).h;
  24.  
  25. // Set the initial y position to 10
  26. var y = 10;
  27.  
  28. // Loop through the lines
  29. for (var i = 0; i < lines.length; i++) {
  30. // If the line would exceed the height of the page, add a new page
  31. if (y + lineHeight > pageHeight - 10) {
  32. pdf.addPage();
  33. y = 10;
  34. }
  35.  
  36. // Insert the line into the document
  37. pdf.text(lines[i], 10, y);
  38.  
  39. // Update the y position
  40. y += lineHeight;
  41. }
  42.  
  43. // Save the PDF document
  44. pdf.save(title + ".pdf");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement