X marks the spot – a print job

10

0

Given two numbers, print a page with an "X" in the specified location (±10%).

The numbers must be either percentages or fractions of the page's print area dimensions, e.g. [25,75] or ["15%","0%"] or [0.33,1]. You do not have to consider margins. Just remember to state which number is width, and which is height, and which corner is the origin.

You may of course assume that a printer is connected, and any virtual printer counts too. In fact, you may want to set a PDF printer as your default printer before beginning this challenge.

You may create a printable file (e.g. a PDF) and send that to a virtual printer, which in turn creates your result PDF, but you may not directly create the final PDF. This has to be a proper print job.

If possible, please include a photo, scan, or screenshot of the result, or link to an online document. Note that Imgur accepts PDFs.

You will of course get more upvotes if you actually place an X rather than outputting newlines and spaces with an assumed font and paper size.

Example

For input height 75% and width 25%, from top left, print the following document:


[25,75]


Adám

Posted 2017-05-16T08:27:49.777

Reputation: 37 779

Are we allowed to read the image file from anywhere? – Beta Decay – 2017-05-16T09:38:33.430

@BetaDecay What image file? – Adám – 2017-05-16T09:38:57.513

The image in your question: https://i.stack.imgur.com/rfZeO.png

– Beta Decay – 2017-05-16T11:00:33.633

@BetaDecay You are supposed to generate a document, not read an image. – Adám – 2017-05-16T11:01:59.820

What paper size should we use/assume, or does it matter? – Stephen – 2017-05-16T14:26:24.247

@StephenS Doesn't matter. Brownie points for handling any (given?) size. – Adám – 2017-05-16T14:27:06.357

Answers

6

MacOS Bash + OfficeJet Pro 8600, 46

(yes ''|sed $[$2*3/5]q;printf %$[$1*4/5]sX)|lp

Origin is the top-left corner.

I did a test print to check the character dimensions of what gets printed by lp to my printer - it appears to be 82w x 64h, so (integer) multiplying percentages by 3/5 and 4/5 respectively gets within the 10% tolerance.

Result, with command-line input of 25 75:

enter image description here

Digital Trauma

Posted 2017-05-16T08:27:49.777

Reputation: 64 644

6Whoa, must be the first physical object submission on PPCG. – Adám – 2017-05-16T17:12:17.483

@Adám nope - I had some on my printer question too :)

– Digital Trauma – 2017-05-16T17:43:58.823

1

@Adám what about this?

– NoOneIsHere – 2017-05-16T17:52:09.497

1

APL (Dyalog) on Windows, 36 35 bytes

Anonymous function taking (Y,X) in percent from top left as argument

{'X.'⎕WC'Text'(2↓'X'⎕WC'Printer')⍵}

We begin with a list of three elements:

{ an anonymous function (all variables are local)

 the argument, e.g. 75 25

( to the right of

  'X'⎕WC'Printer'Windows Create a Printer object named X, returning '#.X'

  2↓ drop two characters to get just 'X'

) to the right of

'Text' what we will add

'X.'⎕WCWindows Create a (type, content, position) object in the object X, returning '#.X'

} end of function (since X is local, it is destroyed when the function terminates, signalling to Windows that we are done with the print job, which causes Windows to print it)


X @ 75 25

Adám

Posted 2017-05-16T08:27:49.777

Reputation: 37 779

0

Java 8, 210 209 bytes

-1 byte: changed to currying syntax

import java.awt.print.*;
x->y->{PrinterJob j=PrinterJob.getPrinterJob();j.setPrintable((g,p,i)->{g.drawString("X",(int)(p.getWidth()*x),(int)(p.getHeight()*y));return i;});try{j.print();}catch(Exception e){}};

Takes input as floating-point version of percent: 0.25, 0.75.

If paper size can be assumed as 8.5x11in (72dpi), 184 bytes

import java.awt.print.*;
x->y->{PrinterJob j=PrinterJob.getPrinterJob();j.setPrintable((g,p,i)->{g.drawString("X",x*612/100,y*792/100);return i;});try{j.print();}catch(Exception e){}};

Uses default values for printing to PDF. Now takes input as integer version of percent: 25, 75.

If a Windows OS can also be assumed, 177 bytes

import sun.awt.windows.*;
x->y->{WPrinterJob j=new WPrinterJob();j.setPrintable((g,p,i)->{g.drawString("X",x*612/100,y*792/100);return i;});try{j.print();}catch(Exception e){}};

Same inputs as above: 25, 75.

Usage

import java.awt.print.*;
...
Function<Double, Consumer<Double>> f =
x->y->{PrinterJob j=PrinterJob.getPrinterJob();j.setPrintable((g,p,i)->{g.drawString("X",(int)(p.getWidth()*x),(int)(p.getHeight()*y));return i;});try{j.print();}catch(Exception e){}};
...
f.apply(.25).accept(.75);

Test Case

For input of 0.25, 0.75:

Java Printing Test Case

(I don't have a physical printer, but this should still work without issue)

Justin Mariner

Posted 2017-05-16T08:27:49.777

Reputation: 4 746

Very impressive. Why do you need catch(Exception e){}? – Adám – 2017-05-23T04:43:44.277

@Adám Because PrinterJob.print() throws a PrinterException and I didn't see any way to get around it.

– Justin Mariner – 2017-05-23T10:19:14.153

0

C#, 259 202 201 bytes

namespace System.Drawing.Printing{w=>h=>{var p=new PrintDocument();p.PrintPage+=(s,e)=>e.Graphics.DrawString("X",new Font("Onyx",9),Brushes.Red,e.PageBounds.Width*w,e.PageBounds.Height*h);p.Print();};}

Onyx is a font installed on my Windows 10 machine by default where 4 seems to be the shortest font name. I tested that by running the following in C# and then inspecting blah in the debugger.

var blah = FontFamily.Families.GroupBy(o => o.Name.Length)
                              .OrderBy(g => g.Key);

Full/formatted version:

namespace System.Drawing.Printing
{
    class P
    {
        static void Main(string[] args)
        {
            Func<float, Action<float>> f = w => h =>
            {
                var p = new PrintDocument();
                p.PrintPage += (s, e) =>
                    e.Graphics.DrawString("X", new Font("Onyx", 9), Brushes.Red,
                                          e.PageBounds.Width * w, e.PageBounds.Height * h);
                p.Print();
            };

            f(0.25f)(0.75f);
        }
    }
}

Test case (0.25, 0.75):


Example document

TheLethalCoder

Posted 2017-05-16T08:27:49.777

Reputation: 6 930