can somebody help me with this... i need a javacript code to print multiple documents when a print button is pressed. I posted 20 documents on my site and i just wanted to have a print button underneath the links of all the documents so the viewers can just print all the documents in a press of a button. I just dont want them to open those documents and print them one by one. Coz all the documents are basically have to be printed anyway. Thanks a lot!!!

i have this code but this code opens up a dialog box for each new window and is annoying. i just need one print dialog box for ALL the pages


function print(url,name)
{
var win=window.open(url,name,"width=100,height=100");
win.opener.focus()
win.print();
}

function printAll()
{
print("about:blank","win1") \\about blank is a sample url
print("about:blank","win2")
print("about:blank","win3")
print("about:blank","win4")
print("about:blank","win5")
print("about:blank","win6")
print("about:blank","win7")
print("about:blank","win8")
}

<form>
<input name="Print" onClick="printAll(); return false" type="button" value="Print">
</form>

Dani AI

Generated

Short answer: don't try to open and print 20 separate windows. Put all pages into one printable bundle (PDF or one long HTML) and call print once. That gives a single print dialog and a predictable ordering for non-technical users.

A reliable server-side workflow (best for seniors) is:

  • Convert any Office docs to PDF (LibreOffice headless works well).
  • Merge the PDFs into one file.
  • Serve that single PDF with a "Print" button (or open it in a new tab and let users hit Print).

Example shell steps you can run on the server:

# convert .docx to PDF
libreoffice --headless --convert-to pdf --outdir /tmp /path/to/file.docx

# merge PDFs with pdftk
pdftk file1.pdf file2.pdf file3.pdf cat output all-docs.pdf

If you cannot do server-side conversion, a client-side option (same-origin only) is to fetch each HTML fragment, concatenate them into a single document, apply print CSS and page breaks, open one window and call print once. Example pattern:

async function printBundle(urls){
  const w = window.open('','printBundle');
  let html = '<html><head><style>@media print{.page{page-break-before:always}}</style></head><body>';
  for(const u of urls){
    const r = await fetch(u);
    html += '<div class="page">'+ await r.text() +'</div>';
  }
  html += '</body></html>';
  w.document.write(html); w.document.close(); w.focus(); w.print();
}

Notes and gotchas: binary Office files can't be fetched/rendered in the browser — convert to PDF or HTML first. Cross-origin requests will be blocked unless the documents are same-origin or CORS-enabled. Test the merged output for page breaks and headers/footers; use page-break-before: always (or break-before) to force document boundaries. For truly silent printing you need a controlled environment (kiosk/browser policy or a small native helper app), which is more complex and outside normal web security. This builds on ’s PDF idea and matches ’s move to PDFs while giving practical ways to produce a single, user-friendly print job.

Recommended Answers

All 2 Replies

Sorry. Welcome to the world of the browser. The user is God of this world. Meaning, that you can't script an automated print. That would be "hijacking" control away from the user. The JavaScript "print" method sends a request to the browser. The browser responds by opening the Print Dialog for the user. The user can decide if/where/how they want to print.

Now, there is a rather convoluted work-around. I coded a system for a financial firm that did automated document/form processing. Users would browse to a PDF form, fill it out, and the server would process/validate the information, store in a database, and return the PDF to the user with corrected data. It would then automatically print, silently, to the default printer.

The key is to use a PDF. Embed a PDF JavaScript into the PDF. While the browser object model doesn't have a silent print method, the Acrobat/PDF object model DOES.

That only addresses part of your problem, in that it's still a document-at-a-time.

Perhaps a totally different approach. Provide a zip file with all documents, that the user can download to the desktop and do with what they choose.

Thanks a lot for the info tgreer. I see what your saying. Actually i thought about zipping them too into a file. but the reason i didnt do it is because most of the users that will gain access to my site are senior citizens and maybe most of them arent familiar with zip files. While most of the links are office docs, i converted them into a pdf file so all they have to do is open the pdf and print it.looks like its doing pretty good.now my job is to arrange them in order which is like 50 pages. but hey, it works...thanks again mayn for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.