Skip to content

Commit f5ade7a

Browse files
committed
try adding some code for print :/
1 parent 529c437 commit f5ade7a

File tree

1 file changed

+135
-2
lines changed

1 file changed

+135
-2
lines changed

windows/RNPrint/RNPrint.cpp

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
#include "pch.h"
22

33
#include "RNPrint.h"
4+
#include "winrt/Microsoft.ReactNative.h"
5+
#include <winrt/Windows.Graphics.Printing.h>
6+
#include <winrt/Windows.Storage.h>
7+
#include <winrt/Windows.Storage.Streams.h>
8+
#include "winrt/Windows.Foundation.h"
9+
#include <winrt/Windows.System.h>
10+
#include <winrt/Windows.Web.Http.h>
11+
#include <winrt/Windows.Storage.h>
12+
13+
using namespace winrt;
14+
using namespace winrt::Microsoft::ReactNative;
15+
using namespace winrt::Windows::Foundation;
416

517
namespace winrt::RNPrint
618
{
@@ -11,7 +23,128 @@ void RNPrint::Initialize(React::ReactContext const &reactContext) noexcept {
1123
m_context = reactContext;
1224
}
1325

14-
void RNPrint::Print(RNPrintCodegen::RNPrintSpec_RNPrintOptions&& options, ::React::ReactPromise<::React::JSValue>&& promise) noexcept {
15-
}
26+
void RNPrint::Print(RNPrintCodegen::RNPrintSpec_RNPrintOptions&& options, ::React::ReactPromise<::React::JSValue>&& promise) noexcept
27+
{
28+
try
29+
{
30+
if (!options.filePath.has_value()) {
31+
promise.Reject(L"Only filePath printing is supported without XAML controls.");
32+
return;
33+
}
34+
35+
std::string filePathStr = options.filePath.value();
36+
auto filePathHstring = winrt::to_hstring(filePathStr);
37+
38+
// Check if the filePath is a URL (http/https)
39+
if (filePathStr.rfind("http://", 0) == 0 || filePathStr.rfind("https://", 0) == 0) {
40+
// Download the file to a temporary location first
41+
m_context.UIDispatcher().Post([filePathStr, promise = std::move(promise)]() mutable {
42+
using namespace winrt::Windows::Storage;
43+
using namespace winrt::Windows::Web::Http;
44+
using namespace winrt::Windows::Foundation;
45+
46+
try {
47+
auto tempFolder = ApplicationData::Current().TemporaryFolder();
48+
auto uri = winrt::Windows::Foundation::Uri(winrt::to_hstring(filePathStr));
49+
HttpClient httpClient;
1650

51+
httpClient.GetBufferAsync(uri).Completed(
52+
[tempFolder, uri, promise = std::move(promise)](
53+
IAsyncOperationWithProgress<winrt::Windows::Storage::Streams::IBuffer, winrt::Windows::Web::Http::HttpProgress> op,
54+
AsyncStatus status) mutable
55+
{
56+
if (status != AsyncStatus::Completed) {
57+
promise.Reject(L"Failed to download file for printing.");
58+
return;
59+
}
60+
auto buffer = op.GetResults();
61+
auto path = uri.Path();
62+
std::wstring fileName = L"printfile";
63+
if (!path.empty()) {
64+
std::wstring wpath = path.c_str();
65+
size_t pos = wpath.find_last_of(L"/\\");
66+
if (pos != std::wstring::npos && pos + 1 < wpath.size()) {
67+
fileName = wpath.substr(pos + 1);
68+
}
69+
else {
70+
fileName = wpath;
71+
}
72+
}
73+
tempFolder.CreateFileAsync(fileName, CreationCollisionOption::GenerateUniqueName).Completed(
74+
[buffer, promise = std::move(promise)](IAsyncOperation<StorageFile> fileOp, AsyncStatus fileStatus) mutable {
75+
if (fileStatus != AsyncStatus::Completed) {
76+
promise.Reject(L"Failed to create temp file for printing.");
77+
return;
78+
}
79+
StorageFile file = fileOp.GetResults();
80+
FileIO::WriteBufferAsync(file, buffer).Completed(
81+
[file, promise = std::move(promise)](IAsyncAction /*writeOp*/, AsyncStatus writeStatus) mutable {
82+
if (writeStatus != AsyncStatus::Completed) {
83+
promise.Reject(L"Failed to write to temp file for printing.");
84+
return;
85+
}
86+
winrt::Windows::System::LauncherOptions options;
87+
options.DisplayApplicationPicker(false);
88+
options.PreferredApplicationDisplayName(L"Print");
89+
options.PreferredApplicationPackageFamilyName(L"");
90+
91+
winrt::Windows::System::Launcher::LaunchFileAsync(file, options).Completed(
92+
[promise = std::move(promise)](IAsyncOperation<bool> op, AsyncStatus status) mutable {
93+
if (status == AsyncStatus::Completed && op.GetResults()) {
94+
promise.Resolve(::React::JSValue(true));
95+
}
96+
else {
97+
promise.Reject(L"Failed to launch print handler for file.");
98+
}
99+
}
100+
);
101+
}
102+
);
103+
}
104+
);
105+
}
106+
);
107+
}
108+
catch (...) {
109+
promise.Reject(L"Exception occurred while downloading file for printing.");
110+
}
111+
});
112+
} else {
113+
// Local file path
114+
m_context.UIDispatcher().Post([filePathHstring, promise = std::move(promise)]() mutable {
115+
using namespace winrt::Windows::Storage;
116+
using namespace winrt::Windows::Foundation;
117+
118+
StorageFile::GetFileFromPathAsync(filePathHstring).Completed(
119+
[promise = std::move(promise)](IAsyncOperation<StorageFile> op, AsyncStatus status) mutable {
120+
if (status != AsyncStatus::Completed) {
121+
promise.Reject(L"Failed to open file for printing.");
122+
return;
123+
}
124+
StorageFile file = op.GetResults();
125+
126+
winrt::Windows::System::LauncherOptions options;
127+
options.DisplayApplicationPicker(false);
128+
options.PreferredApplicationDisplayName(L"Print");
129+
options.PreferredApplicationPackageFamilyName(L"");
130+
131+
winrt::Windows::System::Launcher::LaunchFileAsync(file, options).Completed(
132+
[promise = std::move(promise)](IAsyncOperation<bool> op, AsyncStatus status) mutable {
133+
if (status == AsyncStatus::Completed && op.GetResults()) {
134+
promise.Resolve(::React::JSValue(true));
135+
} else {
136+
promise.Reject(L"Failed to launch print handler for file.");
137+
}
138+
}
139+
);
140+
}
141+
);
142+
});
143+
}
144+
}
145+
catch (...)
146+
{
147+
promise.Reject(L"Unknown error in Print function");
148+
}
149+
}
17150
} // namespace winrt::RNPrint

0 commit comments

Comments
 (0)