print "Hello, World!"

79

10

Yes, really. Print it with a hardware printer to create a hard copy.

Send the necessary commands/data to your printer such that the following text is printed on a piece of paper:

Hello, World!

You may use any local- or network-connected printer that is available to you.

If your printer allows, you should use 8pt or greater text in black ink on white paper.

The text should appear on its own line. It may be left or right positioned as you wish.

Digital photographic/video/GIF evidence of submissions will be helpful.

If your hardware cannot do paper, but allows other exotic hardcopy production such as 3D printing, CNC machining, or printing at the atomic level, then I will happy to accept such interesting submissions.


To clarify, the question asks "Send the necessary commands/data to your printer". The implication of this is that no further user input should be required. Opening a print dialog is not sufficient. Existing answers that do this as of 10:48am PST 31st Dec 2016 will be grandfathered (but not upvoted by me).

Digital Trauma

Posted 2016-12-30T21:37:02.273

Reputation: 64 644

56Please supply address to which we can send submissions for verification. – steenbergh – 2016-12-30T21:38:08.810

1@steenbergh Yep - good point - Digital photographic/video/GIF evidence of submissions should be good enough. – Digital Trauma – 2016-12-30T21:41:09.043

3It's worth noting that many older printers use custom command sets. Most of these print ASCII characters literally, so if you have access to one of the printers in question (or can just claim that you do), sending Hello, World! to the printer port should be enough. – None – 2016-12-30T22:30:38.343

1I could swear that I tried print("Hello, World!") in the Firefox console once and it opened the print dialogue (useless because I wasn't connected to a printer in any way), but now it just returns the string... – ETHproductions – 2016-12-31T01:54:33.130

are xditview, distiller, cups-pdf and other printer simulators considered cheating? – Jasen – 2016-12-31T04:52:33.707

@Jasen Those are fine for testing, so long as your answer will work with a real printer with no modification – Digital Trauma – 2016-12-31T04:59:31.823

2@ETHproductions in chrome doing <script>print("Hello, World!")</script> does open the print dialog, but without the actual text on it. – FlipTack – 2016-12-31T10:23:28.687

Can submissions open a print dialog box that has to be submitted before the text is printed? Some languages (e.g. JavaScript) can't compete otherwise. – ETHproductions – 2016-12-31T13:03:23.360

@ETHproductions No, I want complete automation without additional user input. – Digital Trauma – 2016-12-31T18:43:18.603

After I read the title, I thought the question was about Python 2. :-( – palsch – 2017-01-01T20:37:54.930

Too bad we still don't have postscript answer :-( – val says Reinstate Monica – 2019-07-14T19:55:11.910

Answers

41

Bash, 21 19 bytes

lp<<<Hello,\ World!

actual printout

And there it is.

2 bytes saved thanks to Dennis!

Blue

Posted 2016-12-30T21:37:02.273

Reputation: 26 661

1+1 Valid as a script. Works fine on my Mac. If you want to run it directly at the command-line you'll need to change the double-quotes to single-quotes. – Digital Trauma – 2016-12-30T21:45:10.823

why not have banner to make ot look pretty – juggernauthk108 – 2017-01-01T19:43:13.447

@juggernaut1996 where? Not if I have to print something out again – Blue – 2017-01-01T19:46:11.093

37

PowerShell, 18 bytes

'Hello, World!'|lp

Try it online!

enter image description here

briantist

Posted 2016-12-30T21:37:02.273

Reputation: 3 110

9Not sure the point of the TIO link. +1 anyway. – Digital Trauma – 2016-12-31T00:20:16.793

1@DigitalTrauma I thought it was funny; and it generates the post for me. You can also look at the debug output just to see that it's trying (and failing) to spool the job. :-p – briantist – 2016-12-31T00:21:10.447

19@DigitalTrauma If you put your address in the input field, TIO will mail you the result. – Dennis – 2016-12-31T00:23:18.503

@Dennis if I had money to burn I'd submit an answer that uses an API-to-print service. Alas... – briantist – 2016-12-31T00:25:42.477

19

QBIC, 11 bytes

'LPRINT `_h

Almost forgot I built a 'Hello, World!' command into QBIC...

'          Starts a code literal. This used to be "$", finally changed it.
 LPRINT `  Feeds everything from ` to the next ` directly to QBasic. 
           In this case, "LPRINT" and a space
_h         Yields "Hello, World!"

Tested and found working identical to my QBasic answer: enter image description here

steenbergh

Posted 2016-12-30T21:37:02.273

Reputation: 7 772

1um.. so what's that link? Is it a language invented by you? – eis – 2016-12-30T22:47:44.220

1@eis Yes, I made QBIC. The link is a Google Drive folder with all the code in it - I suck at GitHub... It's an interpreter atop of QBasic (QBIC stands for Quick Basic's Interpreter for Codegolf) and it runs in DosBox. Check out the Showcase-thread for some examples (or just open my profile's answers-page, 75% is in QBIC). WIP, so syntax and tokens occasionally get shifted. – steenbergh – 2016-12-30T22:50:22.903

The OP used a capital "W" to avoid builtin "hello world" (see also).: building your own builtin command for the job is not code golfing, or you could make it "0 bytes" with a builtin default behavior for that.

– Cœur – 2017-01-02T04:45:31.697

2@Cœur: that hardly works nowadays, most newer golfing languages use the capital W precisely because it's standard on PPCG, and it's highly likely that the command was added with no knowledge of this particular challenge (just a guess that more "Hello, World!" challenges would be submitted in the future) – None – 2017-01-02T11:01:26.450

@Cœur Welcome to PPCG! You may be surprised to learn that you're not the first person to whom that particular concern has occurred. Here's a good place to start reading: http://meta.codegolf.stackexchange.com/q/4867/11261 And further, people who enjoy code golfing spend a lot of time thinking about loopholes: http://meta.codegolf.stackexchange.com/q/1061/11261

– Jordan – 2017-01-03T04:32:17.983

16

Batch, 22 bytes

echo Hello, World!>prn

This redirects the output of the echo command to the device file PRN, meaning that it's sent directly to the default printer (usually LPT1).

I neither have a DOS machine nor a printer, so I'm afraid I can't test this right now, but I'm fairly certain I've done this back in the day. It doesn't work on Windows XP or later.

Dennis

Posted 2016-12-30T21:37:02.273

Reputation: 196 637

This reminds of when I tried to create a file called con and was unable to. Ah, the joy of DOS device files... – aross – 2017-01-02T14:02:00.020

NT (and presumable XP etc) can be configured to print this way using the "net" command. iirc "net use lpt1: something" – Jasen – 2017-02-04T10:28:25.830

16

HTML + Javascript, 37 34 bytes

<body onload=print()>Hello, World!

Tested in Safari:

enter image description here

Thanks to @jimmy23013 for some savings.

fluffy

Posted 2016-12-30T21:37:02.273

Reputation: 725

11This appears just to open the print dialog box and not actually send the job to the printer without further manual intervention – Digital Trauma – 2016-12-31T05:00:33.387

Minor nitpick, the W in world should be capitalized. – Pavel – 2016-12-31T07:21:07.200

4@DigitalTrauma JS cannot do that – Mr. Alien – 2016-12-31T11:07:34.603

Also, you can shave 9 bytes off by omitting </script>. – Erik the Outgolfer – 2016-12-31T14:35:14.603

1Are you sure, @EriktheOutgolfer? Firefox and Chrome evaluates the script code only when the closing tag is met. – manatwork – 2016-12-31T16:41:29.513

@manatwork Oh, I can't remember well, I just recalled from ≥1 year ago. – Erik the Outgolfer – 2016-12-31T16:52:01.340

1Yeah I tried leaving off the </script> and it didn't work. – fluffy – 2016-12-31T17:54:00.907

2-3 bytes: <body onload=print()>Hello, World! – jimmy23013 – 2017-01-01T06:38:50.847

@jimmy23013 Nice! I thought there might be some trick like that but I couldn't quite think what. – fluffy – 2017-01-01T09:48:47.480

13

hello + lp + tr, 9 15 bytes

hello|tr w W|lp

I originally had

hello|lp

But it had the wrong capitalisation on the w

hello is gnu hello from the Debian package hello

Jasen

Posted 2016-12-30T21:37:02.273

Reputation: 413

7hello prints a lowercase w, as is the custom on most programming sites. (I'm not quite sure how the uppercase W became standard on PPCG.) – None – 2016-12-31T06:25:50.463

7@ais523 I think uppercase W is standard here precisely to avoid built ins like this. – Pavel – 2016-12-31T07:20:02.077

2If the capitalization is wrong, I'm afraid this is invalid. – Dennis – 2016-12-31T16:25:32.013

Thanks for your comments, I will delete this answer tomorrow. – Jasen – 2017-01-01T04:40:08.537

6How about hello|tr w W|lp? – David Conrad – 2017-01-01T13:32:53.617

13

Python 2.7, 421 bytes

import zlib,base64,os;os.system("s=$'%s';lp<<<\"$s\""%zlib.decompress(base64.decodestring("eNqVkk1qxDAMha/yupNgwNfoDboReDPdhQRmrcNXP3bixA3tGEMesj/r5wXoq+YysUemI0BWlYgV\npTyAEDKEQSDucxLxJaj6gUVKE8BFsH2TIOM5iMyrcTIL3YnMqCH4X0TLONTwF3H04Z0XuRPeR3Wi\nxDOi1EZY7gUTWFa8s+z5kTgcnK3sBtbZQRtCt5LPDlrliKouDh5DYz07KB6COuETUL/YRthGxHqZ\nbjyWBAU8EFk6z350Yt97Dol65hxUow9i3zr8YGxFS61nB4szPqvDnS7CU/nFwYLIYczn97JsD3xt\nr+X5wT/ARNN3\n")))

Seems a bit too long? Perhaps it's a bit overcomplicated.... :P

A lot of hello world

In all seriousness, here is a shorter one:

import os;os.system("lp<<<'Hello, World!'")

Not very interesting, though.

Calconym

Posted 2016-12-30T21:37:02.273

Reputation: 459

Relevant meta discussion: What additional information should be allowed in a submission?

– Dennis – 2016-12-31T16:13:36.687

11

ZX Spectrum BASIC, 16 bytes

LPRINT "Hello, World!"

LPRINT is a 1-byte keyword in ZX Spectrum BASIC, having codepoint 224.

Neil

Posted 2016-12-30T21:37:02.273

Reputation: 95 035

1can you show a photo of the hardcopy. – Jasen – 2016-12-31T04:56:10.043

4@Jasen In theory I still have the kit around, but it would be a bit of a palaver to dig it out and hook it up to an old enough TV. Sorry about that. – Neil – 2016-12-31T09:50:33.013

you could probably type that command "blind". – Jasen – 2016-12-31T09:56:07.930

10

MATLAB, 40 37 36 bytes

Printing text is not something you'd normally do in Matlab, but it can be done.

title 'Hello, World!'
axis off;print

I saved 3 bytes thanks to Tom Carpenter (use title instead of text(0,0,. I saved an additional 2 bytes by substiting title('Hello, World!') withtitle 'Hello, World!' followed by a newline instead of a semicolon.

title adds a title to a figure. If a figure is not yet open, it will be created.

axis off is used to get rid of the frame and axes, so that the text appears alone.

print prints the current figure to the default printer if no input arguments are given.

I printed this (successfully according to the dialog box) on my default printer at the office. I can retrieve it there and prove it but you'll have to wait two weeks. After changing the default to a pdf-printer, this is the output:

enter image description here

^^ Not the correct image anymore.

Stewie Griffin

Posted 2016-12-30T21:37:02.273

Reputation: 43 471

8

QBasic, 21 bytes

LPRINT"Hello, World!"

LPRINT sends text directly to the printer. Unfortunately untested, DosBox doesn't natively support NET USE, so I can't reroute LPT1: to NovaPDF.

The problems I have...

UPDATE: Tested it in QB64. Resulting PDF opened in Microsoft Edge. Looks like this: enter image description here

steenbergh

Posted 2016-12-30T21:37:02.273

Reputation: 7 772

7

8086 machine code, 28 bytes

00000000  be 0f 01 b9 0d 00 31 d2  ac 98 cd 17 e2 fa c3 48  |......1........H|
00000010  65 6c 6c 6f 2c 20 57 6f  72 6c 64 21              |ello, World!|
0000001c

Uses the standard int 0x17 BIOS call. I don't have my dot-matrix printer set up right now so this code is untested.

How it works:

            |   org 0x100
            |   use16
be 0f 01    |       mov si, msg         ; source pointer = msg
b9 0d 00    |       mov cx, 13          ; counter = length of msg
31 d2       |       xor dx, dx          ; clear dx
ac          |   @@: lodsb               ; al = *si++
98          |       cbw                 ; sign-extend al->ax (simply clears ah)
cd 17       |       int 0x17            ; send char in al to printer dx
e2 fa       |       loop @b             ; loop while (cx-- > 0)
c3          |       ret
48 65 6c    |   msg db "Hello, World!"
6c 6f 2c    |   
20 57 6f    |   
72 6c 64    |   
21          |   

user5434231

Posted 2016-12-30T21:37:02.273

Reputation: 1 576

1Sure, just added it. – user5434231 – 2017-04-22T04:04:10.930

Now we're talking! – Limited Atonement – 2017-05-03T19:24:47.843

6

Batch + Stuck, 12 bytes

Inspired by this answer an empty stuck program prints "Hello, World!".

  1. Have a printer connected to your computers LPT1 port
  2. Create an empty stuck file to be interpreted (filename a)
  3. Run the stuck interpretter on the empty file and pipe the result to LPT1

Code

stuck a>LPT1

Daniel

Posted 2016-12-30T21:37:02.273

Reputation: 1 808

4If you're using multiple languages, you also need to count the glue that connects them together as part of your byte count. I'd count this as 15 for the Batch program, plus 0 due to the need for a 0-byte external file. – None – 2017-01-02T10:59:38.767

1Can you show distinctly the Windows batch script, and explicitly the command used to call Stuck and pass the result to the printer? By explicitly I mean something like stuck empty > LPT1. BTW if this uses a REPL I believe the consensus is that should be clearly stated (I believe it's to minimise confusion exactly like this). – redstarcoder – 2017-01-02T14:39:20.823

@ais523 I conquer - I should count the glue, and because of that, raised it from 5 bytes to 12 – Daniel – 2017-01-03T10:24:01.470

You can save a byte here by redirecting to PRN instead of LPT1. – user5434231 – 2017-04-22T06:10:42.740

1IMO you need to add 1 byte for the 1 byte filename. – CalculatorFeline – 2017-05-30T21:40:32.447

4

GFA-Basic, 22 bytes

Only tested on an Atari ST emulator with the parallel port redirected to a file. It should work on the Windows version of GFA-Basic (which is free), but this is untested.

LPRINT "Hello, World!"

Note: This turns out to be identical to the QBasic syntax.

Arnauld

Posted 2016-12-30T21:37:02.273

Reputation: 111 334

4

Java, 330 bytes

Golfed:

import java.awt.print.*;void f()throws Throwable{PrinterJob job=PrinterJob.getPrinterJob();job.setPrintable(new Printable(){public int print(java.awt.Graphics g,PageFormat f,int i){if(i==0){((java.awt.Graphics2D)g).translate(f.getImageableX(),f.getImageableY());g.drawString("Hello, World!",0,90);}return i>0?1:0;}});job.print();}

Ungolfed (import plus function only):

import java.awt.print.*;

void f() throws Throwable {
  PrinterJob job = PrinterJob.getPrinterJob();
  job.setPrintable(new Printable() {
    public int print(java.awt.Graphics g, PageFormat f, int i) {
      if (i == 0) {
        ((java.awt.Graphics2D) g).translate(f.getImageableX(), f.getImageableY());
        g.drawString("Hello, World!", 0, 90);
      }
      return i > NO_SUCH_PAGE ? 1 : PAGE_EXISTS;
    }
  });
  job.print();
}

Java is not a great golfing language, and certain does an exceptionally poor job golfing anything hardware-related, printing included.

While testing this program, I set my PDF printer as the default. It worked, but also sent a print job to my laser printer containing 87,792 pages of "Hello, World!" I pulled the paper tray and canceled the job, then retested. It did not happen again. Thanks, Windows 10.

user18932

Posted 2016-12-30T21:37:02.273

Reputation:

-7 bytes by changing 3x job to j, and i==0 to i<1. – Kevin Cruijssen – 2017-05-23T14:51:19.650

3

JavaScript, 37 36 bytes

print(document.write`Hello, World!`)

Saved 15 bytes thanks to @manatwork and @xem!

Oliver

Posted 2016-12-30T21:37:02.273

Reputation: 7 160

3No need to explicitly specify window: document.write('Hello, World!');print(). Or even shorter: print(document.write('Hello, World!')). – manatwork – 2016-12-31T11:08:24.410

3

with ES6 you can even do: document.write\Hello, world!` ` (more info here: http://xem.github.io/articles/#webspeech)

– xem – 2016-12-31T13:03:29.993

Does it print without confirmation? No? Well too bad, it's invalid. – CalculatorFeline – 2017-05-30T21:39:07.897

@CalculatorFeline Why the hostility? If you read the footnote of the challenge you'd see "...Existing answers that do this as of 10:48am PST 31st Dec 2016 will be grandfathered...". – Oliver – 2017-05-30T23:40:22.640

Oh. I missed that part. – CalculatorFeline – 2017-05-30T23:50:06.323

2

Mathematica, 29 bytes

NotebookPrint@"Hello, World!"

enter image description here

alephalpha

Posted 2016-12-30T21:37:02.273

Reputation: 23 988

2

c#, 259 250 bytes

using System.Drawing;using System.Drawing.Printing;struct p{static void Main(){var p=new PrintDocument();p.PrintPage+=(s,e)=>e.Graphics.DrawString("Hello,World!",new Font("Arial",12),new SolidBrush(Color.Black),new Rectangle(0,0,999,99));p.Print();}}

Example output

Johan du Toit

Posted 2016-12-30T21:37:02.273

Reputation: 1 524

2

Batch, 32 bytes

echo Hello, World!>t
notepad/P t

Should work on all versions of windows with no manual intervention required.

Johan du Toit

Posted 2016-12-30T21:37:02.273

Reputation: 1 524

1

HTML (33)

Hello, World!<svg onload=print()>

(Prompts a print window in the browser, doesn't print directly)

xem

Posted 2016-12-30T21:37:02.273

Reputation: 5 523

Is there a reason the tag has to be <svg> specifically, or would any tag work? (Many tags have shorter names.) – None – 2016-12-31T09:00:51.877

7afaik, only svg, img (with a valid src), iframe and body have an onload event triggered when the page is loaded. img without src triggers an onerror event. all in all, the shortest we've found is svg onload=... – xem – 2016-12-31T13:02:35.180

The challenge asks for upper case W (not that it changes your byte count). – Martin Ender – 2016-12-31T13:23:32.363

1

Racket 35 bytes

(system("echo 'Hello World!'\|lp"))

rnso

Posted 2016-12-30T21:37:02.273

Reputation: 1 635

1

APL (Dyalog), 50 bytes

Of course we could shell out and use a Batch/Bash solution, but let us instead create a real print job:

{'X.'⎕WC'Text' 'Hello, World!'⍵⊣'X'⎕WC'Printer'}⍳2

⎕WC is Windows Create object

First we create a printer object (a print job) called 'X', then () in that ('X.') we create a text object at the argument of the anonymous function {}. The argument is ⍳2, which gives the first two integers (1 2) and means 1% from the top and 2% from the left. When the anonymous function terminates, all its local variables (X) are destroyed, which signals to Windows that the print job is ready to be printed:


Print job

Adám

Posted 2016-12-30T21:37:02.273

Reputation: 37 779

0

Javascript, 36 bytes

print(document.write`Hello, World!`)

ericw31415

Posted 2016-12-30T21:37:02.273

Reputation: 2 229

This just opens the print dialog. Further manual intervention is required to actually print. Please see the note at the bottom of the question. – Digital Trauma – 2017-04-22T21:05:39.880

0

ZPL (Zebra Programming Language), 25 bytes

Code:

^XA^FDHello, World!^XZ~PS

Try it online!

Explanation:

^XA                        # Start Format
    ^FDHello, World!       # Field Data "Hello, World!"
^XZ                        # End Format

~PS                        # Print Start

Grant Miller

Posted 2016-12-30T21:37:02.273

Reputation: 706

0

JavaScript + HTML, 20 bytes

print()
Hello, World!

Shaggy

Posted 2016-12-30T21:37:02.273

Reputation: 24 623

This just opens the print dialog. Further manual intervention is required to actually print. Please see the note at the bottom of the question. – Digital Trauma – 2017-04-22T21:05:31.763

0

C#, 174 bytes

namespace System.Drawing.Printing{_=>{var p=new PrintDocument();p.PrintPage+=(s,e)=>e.Graphics.DrawString("Hello, World!",new Font("Onyx",9),Brushes.Black,0,0);p.Print();};}

Full/Formatted version:

namespace System.Drawing.Printing
{
    class P
    {
        static void Main(string[] args)
        {
            Action<float> f = _ =>
            {
                var p = new PrintDocument();
                p.PrintPage += (s, e) =>
                    e.Graphics.DrawString("Hello, World!", new Font("Onyx", 9), Brushes.Black, 0, 0);
                p.Print();
            };

            f(0);
        }
    }
}

TheLethalCoder

Posted 2016-12-30T21:37:02.273

Reputation: 6 930