Skip to main content
NativePHP Desktop provides a simple API for interacting with the system clipboard, supporting text, HTML, and image data.

Text Operations

Writing Text

Copy text to the clipboard:
use Native\Desktop\Facades\Clipboard;

Clipboard::text('Hello, World!');

Reading Text

Get text from the clipboard:
$text = Clipboard::text();

echo $text; // Outputs whatever text is currently in the clipboard

HTML Operations

Writing HTML

Copy HTML content to the clipboard:
Clipboard::html('<h1>Hello</h1><p>This is <strong>HTML</strong> content.</p>');
This is useful for rich text editors or when users need to paste formatted content into other applications.

Reading HTML

Get HTML from the clipboard:
$html = Clipboard::html();
When HTML is copied to the clipboard, most applications also copy a plain text version. The HTML version is used when pasting into applications that support rich text.

Image Operations

Writing Images

Copy an image to the clipboard using a file path:
Clipboard::image('/path/to/image.png');
Or use a data URI:
$dataUri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...';
Clipboard::image($dataUri);

Reading Images

Get an image from the clipboard as a data URI:
$imageDataUri = Clipboard::image();

if ($imageDataUri) {
    // Save or process the image
    $imageData = base64_decode(explode(',', $imageDataUri)[1]);
    file_put_contents('clipboard-image.png', $imageData);
}

Clearing the Clipboard

Clear all clipboard contents:
Clipboard::clear();

Practical Examples

use Native\Desktop\Facades\Clipboard;

class ShareController extends Controller
{
    public function copyShareLink($itemId)
    {
        $item = Item::findOrFail($itemId);
        $url = route('share.item', $item);
        
        Clipboard::text($url);
        
        return response()->json([
            'message' => 'Link copied to clipboard!'
        ]);
    }
}

Chaining with Conditionals

The Clipboard class supports Laravel’s Conditionable trait:
Clipboard::when($shouldCopyHtml, function ($clipboard) {
    $clipboard->html('<h1>Formatted</h1>');
}, function ($clipboard) {
    $clipboard->text('Plain text');
});
Reading from the clipboard requires appropriate permissions on some operating systems. Users may need to grant clipboard access permissions on first use.

Build docs developers (and LLMs) love